当键盘弹起的时候,屏幕适配键盘高度的方法

当我们使用TextFiled或者TextView的时候,经常会因为键盘弹起而挡住编辑区域,而中文键盘因为拼音的缘故会两次调用keyboardWillShow的监听,现在我贴出一个我自己理解的方式


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //加入监听
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];

        
    }
    return self;
}


@property (nonatomic,assign)CGRect              tableViewRect;//设置一个rect来保存你想要升高的view的rect

- (void)keyboardWillShow:(NSNotification *)notif {
    self.keyBoardRect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
        [UIView animateWithDuration:0.5 animations:^{
            CGRect tbRect = self.tableViewRect;
            tbRect.origin.y -=self.keyBoardRect.size.height;
            self.tableView.frame = tbRect;
        } completion:^(BOOL finish){
            
        }];
    
}

- (void)keyboardWillHide:(NSNotification *)notif {
    
        [UIView animateWithDuration:0.5 animations:^{
            
            self.tableView.frame = self.tableViewRect;
        } completion:^(BOOL finish){
            
        }];
    
}


用一个临时变量来做一个中转,这样就省去了很多复杂的逻辑,这样就算是中文2次弹起,也不会受任何影响

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值