iOS键盘优化细节


在延展中加入


/**输入工具条底部的约束*/

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputToolBarBottomConstraint;



修改  viewDidLoad方法如下


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //1.监听键盘弹出,把inputToolbar(输入工具条)往上移

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillShow:) name:UIKeyboardWillShowNotification object:nil];

    

    //2.监听键盘退出,inputToolbar恢复原位

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillHide:) name:UIKeyboardWillHideNotification object:nil];

}



在类中加入方法


#pragma mark 键盘显示时会触发的方法

-(void)kbWillShow:(NSNotification *)noti{

    

    //1.获取键盘高度

    //1.1获取键盘结束时候的位置

    CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat kbHeight = kbEndFrm.size.height;

    

    

    //2.更改inputToolbar底部约束

    self.inputToolBarBottomConstraint.constant = kbHeight;

    //添加动画

    [UIView animateWithDuration:0.25 animations:^{

        [self.view layoutIfNeeded];

    }];

    

    

}

#pragma mark 键盘退出时会触发的方法

-(void)kbWillHide:(NSNotification *)noti{

    //inputToolbar恢复原位

    self.inputToolBarBottomConstraint.constant =0;

}



-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值