iPhone控制键盘

有时候当键盘弹出时会把输入框盖住,这时候我们就没法看到自己到底输入的是什么了,这时候我们就需要注册两个事件,那就是在键盘显示的时候 把输入框的位置放在可见的地方 ,当关闭键盘时,在将输入框还原,以下就是解决问题的具体方法,我们只要注册就可以了

 

// Call this method somewhere in your view controller setup code.

- (void)registerForKeyboardNotifications

{

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidShowNotification 

 object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidHideNotification 

 object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasShown:)

name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasHidden:)

name:UIKeyboardDidHideNotification object:nil];

}

 

// Called when the UIKeyboardDidShowNotification is sent.

- (void)keyboardWasShown:(NSNotification*)aNotification

{

self.frame = CGRectMake(0, -90, 320, 480);

}

 

// Called when the UIKeyboardDidHideNotification is sent

- (void)keyboardWasHidden:(NSNotification*)aNotification

{

self.frame = CGRectMake(0, 20 , 320, 460);

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidShowNotification 

 object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidHideNotification 

 object:nil];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值