iOS開發inputView和inputAccessoryView

一、簡介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;

  • 設置座標時只有高度才能起到約束,x、y、weight設置也不會有做用;
  • 整個彈出視圖的高度是inputView.height+inputAccessoryView.height,並且附件視圖會緊貼inputView視圖上面;
  • inputView和inputAccessoryView的子視圖須要有詳細座標約束;
  • UIToolbar繼承UIView,經常使用於附件視圖上,做點擊事件處理;

  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

  • 要設置  self.userInteractionEnabled = YES;重寫- (BOOL)canBecomeFirstResponder{     return YES; } 方法;
  • 這種方法附件的高度能夠修改,可是底部並不會隨inputView高度改變而變化,是固定的;
  • inputView不設置座標,會自適應展現,因爲系統inputView高度固定,也不知道不一樣版本會不會變化,因此就不設置自定義視圖inputView的高度 太高會覆蓋附件,太低會覆蓋不全;
  • 附件的子視圖須要詳細座標約束;

相關文章
相關標籤/搜索