inputView与inputAccessoryView

一.ios的inputView和inputAccessoryView有什么应用场景
1. 自定义一个键盘以及键盘的辅助视图;
2.弹出底部选择器,简洁大方。

二.详解

在UITextField和UITextView中能查到这两个属性
@property (readwrite, retain) UIView *inputView;
@property (readwrite, retain) UIView *inputAccessoryView;

在UITextField或者UITextView成为默认响应的时候,会弹出系统键盘。如果对这两个控件的inputView属性设置了自定义的view,在其成为第一响应的时候系统键盘将不再弹出,取而代之的是赋值给inputView的那个view。inputAccessoryView是键盘的辅助视图,即键盘上面那部分,同样当对inputAccessoryView设置了自定义view时,键盘弹出的同时,该view会作为辅助视图出现在键盘的上面,和键盘一起弹出。通常会使用pickerView作为自定义的弹出视图,这里为了简单起见,用一个普通的view演示:

//
//  ViewController.m
//  输入视图
//
//  Created by James Qin on 17/11/1.
//  Copyright © 2017年 QinPingjian. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *customInputView;//键盘区域
@property (nonatomic, strong) UIToolbar *customAccessoryView;//键盘辅助视图

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)doClick:(id)sender {

    [self.view addSubview:self.textField];

}



- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 250, self.view.frame.size.width - 100, 30)];
        _textField.layer.borderWidth = 1.0;
        _textField.layer.borderColor = [UIColor redColor].CGColor;
        _textField.layer.cornerRadius = 4.0;
        _textField.placeholder = @"测试";
        _textField.inputView = self.customInputView;
        _textField.inputAccessoryView = self.customAccessoryView;
    }
    return _textField;
}


- (UIView *)customInputView{
    if (!_customInputView) {
        _customInputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 220)];
        _customInputView.backgroundColor = [UIColor lightGrayColor];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 40)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"自定义inputView";
        [_customInputView addSubview:label];
    }
    return _customInputView;
}

- (UIToolbar *)customAccessoryView{
    if (!_customAccessoryView) {
        _customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,self.view.frame.size.width,40}];
        _customAccessoryView.barTintColor = [UIColor orangeColor];
        UIBarButtonItem *cancle = [[UIBarButtonItem alloc]initWithTitle:@"cancle" style:UIBarButtonItemStylePlain target:self action:@selector(cancle)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
        [_customAccessoryView setItems:@[cancle,space,finish]];
    }
    return _customAccessoryView;
}

- (void)done{
    if (self.textField.isFirstResponder) {

        [self.textField resignFirstResponder];
    }
}

- (void)cancle{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

三.对其他控件设置inputView和inputAccessoryView
除了UITextField和UITextView,有事我们可能想让别的控件实现类似效果,比如点击一个按钮、选中某个cell的时候弹出键盘,但是对于UITextField和UITextView以外的控件,inputView和inputAccessoryView是只读的
@property (nonatomic, readonly, retain) UIView *inputView
@property (nonatomic, readonly, retain) UIView *inputAccessoryView
因此如果要使用控件这两个属性,就要创建该控件的子视图,然后重新申明inputView和inputAccessoryView为readwrite的,并重写它们的get方法,这样在UITableViewCell成为第一响应的时候会自动弹出inputView和inputAccessoryView。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页