一、簡介atom
起初看到這兩個屬性是在UIResponder中,只是可讀的:spa
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputView NS_AVAILABLE_IOS(3_2); @property (nullable, nonatomic, readonly, strong) __kindof UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);
後來在UITextField和UITextView中也有,是可讀可寫的:3d
@property (nullable, readwrite, strong) UIView *inputView;
@property (nullable, readwrite, strong) UIView *inputAccessoryView;
用一張輸入法的圖片簡單說明一下這兩個屬性:code
inputAccessoryView:附件視圖,就是上面漢子和拼音component
inputView:就是下面的按鍵輸入法orm
最上面的輸入框:以前做法——監聽鍵盤的通知,獲取鍵盤高度處理輸入框位置;blog
如今——是否能夠把輸入框放入到附件視圖inputAccessoryView?只是一種想法,後期我會本身試試 勿噴!繼承
二、自定義inputAccessoryView和inputView事件
2.一、直接賦值UITextField的這兩個屬性圖片
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 0, 90)]; UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(endEdi:)]; UIBarButtonItem *right2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(endEdi:)]; toolBar.items = [NSArray arrayWithObjects:right,right2,right,right,right,right,right, nil]; self.inputTextField.inputAccessoryView = toolBar; UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 320)]; UIImageView *igView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 180)]; igView.image = [UIImage imageNamed:@"pic2.png"]; [inputView addSubview:igView]; inputView.backgroundColor = [UIColor greenColor]; self.inputTextField.inputView =inputView;
2.二、自定義控件的這兩個屬性
每每會用這兩個屬性來自定義鍵盤,其實能夠自定義許多底部的彈出框!
因爲UIResponder有這兩個屬性,全部大多數視圖能夠重寫這個兩個屬性,以UILabel爲例:
@interface MenuLabel()<UIPickerViewDelegate,UIPickerViewDataSource> { UIToolbar *_inputAccessoryView; UIPickerView *_inputView; } @end @implementation MenuLabel - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self setUI]; } return self; } - (void)awakeFromNib{ [super awakeFromNib]; self.multipleTouchEnabled = YES; [self setUI]; } - (void)setUI{ self.userInteractionEnabled = YES; UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)]; [self addGestureRecognizer:tap]; } - (void)tapGes:(UITapGestureRecognizer *)ges{ [self becomeFirstResponder]; } -(UIView *)inputAccessoryView{ if(!_inputAccessoryView) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)]; view.backgroundColor = [UIColor grayColor]; UIToolbar *toolBar = [[UIToolbar alloc]init]; toolBar.frame = CGRectMake(0, 0, 100, 44); UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dodo)]; toolBar.items = [NSArray arrayWithObject:right]; [view addSubview:toolBar]; return view; } return _inputAccessoryView; } -(UIPickerView *)inputView{ if(!_inputView) { UIPickerView * pickView = [[UIPickerView alloc]init]; pickView.delegate =self; pickView.dataSource = self; pickView.showsSelectionIndicator = YES; return pickView; } return _inputView; } -(void)dodo{ [self resignFirstResponder]; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [NSString stringWithFormat:@"%ld",row]; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return 5; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { self.text =[NSString stringWithFormat:@"%ld",row]; } - (BOOL)canBecomeFirstResponder{ return YES; } @end