UITextField的inputView和inputAcces

作者: 商鞅福泽谕吉 | 来源:发表于2017-10-16 18:04 被阅读104次

    在UITextField中能看到两个属性:
    @property (readwrite, retain) UIView *inputView;
    @property (readwrite, retain) UIView *inputAccessoryView;

    在UITextField称为第一相应者的时候,会弹出系统默认的键盘,那么有时候并不想让系统默认弹出键盘,而是弹出自己自定义的view这种情况下就用到了UITextField这个控件的inputView属性了,我们可以自定义view,使其成为点击textfield默认弹出的自定义view。那么inputAccessoryView是什么呢,踏实键盘的辅助视图,机键盘上面那部分,同样当对inputAccessoryView设置了自定义的view时,键盘弹出的同时,该view会作为辅助视图出现在键盘上面,和键盘一块弹出,默然情况下,inputview在下,inputAccessoryView在上面。

    @interface ViewController ()
    
    @property (nonatomic, strong) UITextField *textField;
    @property (nonatomic, strong) UIView *customInputView;
    @property (nonatomic, strong) UIToolbar *customAccessoryView;
    
    @property (nonatomic, strong) UISearchBar *searchBar;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self createUI];
    }
    - (void)createUI{
        [self.view addSubview:self.textField];
        self.textField.inputView = self.customInputView;
        self.textField.inputAccessoryView = self.customAccessoryView;
        
    }
    - (UITextField *)textField{
        if (!_textField) {
            _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, SCREEN_BOUNDS.size.width - 100, 30)];
            _textField.layer.borderWidth = 1.0;
            _textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
            _textField.layer.cornerRadius = 4.0;
            _textField.placeholder = @"测试";
        }
        return _textField;
    }
    
    - (UIView *)customInputView{
        if (!_customInputView) {
            _customInputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.size.width, 261)];
            _customInputView.backgroundColor = [UIColor lightGrayColor];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_BOUNDS.size.width, 40)];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = @"自定义inputView";
            [_customInputView addSubview:label];
    //        _customInputView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.size.width, 300)];
    //        _customInputView.backgroundColor = [UIColor redColor];
            
        }
        return _customInputView;
    }
    - (UIToolbar *)customAccessoryView{
        if (!_customAccessoryView) {
            _customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCREEN_BOUNDS.size.width,40}];
            _customAccessoryView.barTintColor = [UIColor orangeColor];
            UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            UIBarButtonItem *finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
            [_customAccessoryView setItems:@[space,space,finish]];
        }
        return _customAccessoryView;
    }
    
    - (void)done{
        [self.textField resignFirstResponder];
    }
    
    

    运行结果如图所示:

    自定义inputview和inputAccessoryView

    这里不论改变_customInputView的高度,inputAccessoryView始终在inputView上面。
    再看下面:

    替换代码

    其运行结果如图所示:

    inputAccessoryView被inputview盖住情况

    所以以后如果自定义UITextfield的inputview 那么千万不可吧pickview直接赋值给textfield的inputview否则会出现如图所示的bug。

    相关文章

      网友评论

        本文标题:UITextField的inputView和inputAcces

        本文链接:https://www.haomeiwen.com/subject/uekluxtx.html