美文网首页
在键盘上方添加按钮

在键盘上方添加按钮

作者: yaqiong | 来源:发表于2018-01-18 16:05 被阅读20次
    1.inputAccessoryView

    UITextField和UITextView的这个属性是readwrite的,可以自定义一个view设置为UITextField的inputAccessoryView

    
        UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7,50, 30)];
        [button setTitle:@"完成"forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [toolView addSubview:button];
        
        UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(30, 7,50, 30)];
        [button1 setTitle:@"取消"forState:UIControlStateNormal];
        [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [toolView addSubview:button1];
        _textL.inputAccessoryView = toolView;
    
    

    效果:

    01.jpeg

    当然可以直接将UIView替换成UIToolBar,自带背景色和border:

    
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
    
    

    效果:

    2.jpeg

    不足之处:每次都要为UITextField或UITextView设置inputAccessoryView
    改善:创建继承自UITextField或UITextView的类,在.m的init方法(或者drawRect:方法)统一设置:

    
    - (void)drawRect:(CGRect)rect {
        [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:[UIColor redColor] forState:UIControlStateNormal];
        [bar addSubview:button];
    
        UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(30, 7,50, 30)];
        [button1 setTitle:@"取消"forState:UIControlStateNormal];
        [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [bar addSubview:button1];
        self.inputAccessoryView = bar;
    }
    
    

    自定义TextField需要将按钮的事件传回到控制器执行(block/代理/通知 等方式)

    2.监听键盘弹出和收回

    添加通知:

    [self.view addSubview:self.editView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    

    收到通知之后可以获取键盘的一系列信息:

    
    {
        UIKeyboardAnimationCurveUserInfoKey = 7;
        UIKeyboardAnimationDurationUserInfoKey = "0.25";
        UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
        UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}";
        UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
        UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
        UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
        UIKeyboardIsLocalUserInfoKey = 1;
    }
    
    

    其中UIKeyboardFrameEndUserInfoKey是键盘弹出或收起结束之后的frame
    处理:

    
    - (void)keyboardWillShow:(NSNotification *)noti {
        NSDictionary *userInfo = noti.userInfo;
        CGRect endRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat y = endRect.origin.y;
        [self.editView mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.left.right.mas_equalTo(0);
             make.height.mas_equalTo(44);
             make.top.mas_equalTo(y - 44);
         }];
    }
    
    - (void)keyboardWillHide:(NSNotification *)noti {
        NSDictionary *userInfo = noti.userInfo;            
        CGRect endRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat y = endRect.origin.y;
       [self.editView mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.left.right.mas_equalTo(0);
             make.height.mas_equalTo(44);
             make.top.mas_equalTo(y - 44);
       }];
    }
    

    当然不要忘了移除通知

    相关文章

      网友评论

          本文标题:在键盘上方添加按钮

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