iOS 自定义输入⌨️

使一些控件的键盘变为自定义

#import "ViewController.h"

@interface ViewController ()
// 日期选择控件
@property (nonatomic, strong) UIDatePicker *datePicker;

// 工具条
@property (nonatomic, strong) UIToolbar *toolbar;

@property(nonatomic,strong) UITextField *textField;

@end

@implementation ViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置文本框的输入界面为datePicker
    
    [self.view addSubview:self.textField];
    self.textField.inputView = self.datePicker;
     // 设置工具条
    self.textField.inputAccessoryView = self.toolbar;
    
}
#pragma mark - public methods

#pragma mark - private methods
- (void)cancelItemClick {
    [self.view endEditing:YES];
    
}

- (void)doneItemClick {
    // 1. 获取选中的日期
    NSDate *date = self.datePicker.date;
    
    // 2.将日期设置给文本框
    // 讲日期转为字符串
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy年MM月dd日"; // HH:mm:ss 时分秒
    
    NSString *str = [formatter stringFromDate:date];
    
    // 赋值给文本框
    self.textField.text = str;
    
    
    // 3.关闭键盘
    [self.view endEditing:YES];
    
}
#pragma mark - getter && setter

#pragma mark - lazy loading
- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 300, 50)];
        _textField.borderStyle =  UITextBorderStyleRoundedRect;
    }
    return _textField;
    
}

- (UIToolbar *)toolbar {
    
    if (!_toolbar) {
        // 只需要高度就够了
        _toolbar = [[UIToolbar alloc] init];
        _toolbar.bounds = CGRectMake(0, 0, 0, 44);
        
        // 创建按钮放到工具条里面
        // 取消
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelItemClick)];
        
        // 弹簧
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        // 确认
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(doneItemClick)];
        
        // items里面存放的按钮都是UIBarbuttonItem类型, 而且这些按钮最终是要现实到工具条上的
        _toolbar.items = @[cancelItem, flexSpace, doneItem];
        
        
    }
    return _toolbar;
}

- (UIDatePicker *)datePicker {
    
    if (!_datePicker) {
        // 不需要设置frame 自动占据键盘的位置
        _datePicker = [[UIDatePicker alloc] init];
        // 日期模式
        _datePicker.datePickerMode = UIDatePickerModeTime;
        // 本地化 local
        _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];
    }
    return _datePicker;
    
}

@end
iOS 自定义输入⌨️_第1张图片

你可能感兴趣的:(iOS 自定义输入⌨️)