iOS键盘高度自适应(中英文输入)

本文详细介绍了如何在iOS应用中监听键盘高度变化,并动态调整布局以保持良好的用户体验。通过添加通知来实现键盘出现和消失时的布局自适应,确保应用在不同键盘高度下依然能正常工作。

一:添加通知监测键盘高度变化

   [self keyBoardAutoSize];

二:动态改变高度

#pragma mark keyboard height auto
/*
 NSNotificationCenter:键盘出现、消失时的通知
 
 
 UIKeyboardWillShowNotification;
 UIKeyboardDidShowNotification;
 UIKeyboardWillHideNotification;
 UIKeyboardDidHideNotification;
 
 */

- (void) keyBoardAutoSize
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];

}


- (void)keyBoardShow:(NSNotification *) notif
{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    NSLog(@"keyBoard:%f", keyboardSize.height);  //216
        
    if (keyboardSize.height==216) {
        [UIView animateWithDuration:0.25 animations:^{
            bottomInput.top = kScreenHeight - 74 - keyboardSize.height;
        }];
    }
    //中文状态输入 252
    else if (keyboardSize.height== 252 )
    {
        [UIView animateWithDuration:0.1 animations:^{
            bottomInput.top = kScreenHeight - 74 - keyboardSize.height;
        }];
       
    }
    
 
}

  

三:移除通知

- (void)dealloc

{

    [_hubTip release];

     [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];

    [super dealloc];

}

转载于:https://www.cnblogs.com/cocoajin/p/3246315.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值