美文网首页
iOS11 下键盘右上角完成按钮点击无效处理

iOS11 下键盘右上角完成按钮点击无效处理

作者: dyqian | 来源:发表于2018-01-15 17:06 被阅读0次

    1、重写UITextField子类的drawRect方法

    - (void)drawRect:(CGRect)rect {

    // Drawing code

    [super drawRect:rect];

    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7, 50, 30)];

    [button setTitle:@"完成" forState:UIControlStateNormal];

    [button setTitleColor:UIColorFromHex(0x1784d9) forState:UIControlStateNormal];

    [button addTarget:self action:@selector(doneChoice) forControlEvents:UIControlEventTouchUpInside];

    // ios11 后使用 UIBarButtonItem 管理,不然点击无效

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button]; UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; NSArray *items = @[flexibleitem, flexibleitem, item];

    [bar setItems:items animated:YES];

    self.inputAccessoryView = bar;

    }


    2、storyBoard 中使用方法

    1)控件正常拖线并建立属性

    @property (weak, nonatomic) IBOutlet UITextField *tf_pw;

    2)选择控件继承自你重写的子类

    3、代码实现方法

    @property (nonatomic, strong) JQTextField *tf_pw;

    相关文章

      网友评论

          本文标题:iOS11 下键盘右上角完成按钮点击无效处理

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