iOS 键盘的监听 调整view的位置

本文介绍如何在iOS应用中使用UIKeyboardWillChangeFrameNotifications/UIKeyboardDidHideNotifications来监听键盘的显示和隐藏,通过添加监听器来调整界面布局以适应键盘的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS在处理键盘的出现和消失时需要监听UIKeyboardWillChangeFrameNotifications/UIKeyboardDidHideNotifications

- (void)viewDidLoad
{
    [super viewDidLoad];

//添加监听 键盘消失
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDown) name:UIKeyboardDidHideNotification object:nil];

//添加监听 键盘出现
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}


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

//取出键盘动画的时间(根据userInfo的key-UIKeyboardAnimationDurationUserInfoKey)

    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    


 //取得键盘最后的frame(根据userInfo的key----UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 227}, {320, 253}}";)

    CGRect keyboardFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  
//处理键盘出现的是页面的变化。。。。。。
}


- (void)keyboardDown
{
    //处理键盘消失是页面的变化 。。。。。。
}

 

转载于:https://www.cnblogs.com/carlos-mm/p/5978278.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值