框架类的 键盘监控 消退

- ( void )viewDidLoad
{
[ super viewDidLoad ];
// register for keyboard notifications
[[ NSNotificationCenter defaultCenter ] addObserver : self
selector : @selector (keyboardWillShow:)
name : UIKeyboardWillShowNotification
object : self .view .window ];
// register for keyboard notifications
[[ NSNotificationCenter defaultCenter ] addObserver : self
selector : @selector (keyboardWillHide:)
name : UIKeyboardWillHideNotification
object : self .view .window ];
keyboardIsShown = NO ;
//make contentSize bigger than your scrollSize
//(you will need to figure out for your own use case)
CGSize scrollContentSize = CGSizeMake( 3 2 0 , 3 4 5 );
self .scrollView .contentSize = scrollContentSize;
}
- ( void )viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[ NSNotificationCenter defaultCenter ] removeObserver : self
name : UIKeyboardWillShowNotification
object :nil ];
// unregister for keyboard notifications while not visible.
[[ NSNotificationCenter defaultCenter ] removeObserver : self
name : UIKeyboardWillHideNotification
object :nil ];
}
- ( void )dealloc {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[ NSNotificationCenter defaultCenter ] removeObserver : self
name : UIKeyboardWillShowNotification
object :nil ];
// unregister for keyboard notifications while not visible.
[[ NSNotificationCenter defaultCenter ] removeObserver : self
name :UIKeyboardWillHideNotification
object :nil ];
}
- ( void )keyboardWillHide:( NSNotification *)n
{
NSDictionary* userInfo = [n userInfo ];
// get the size of the keyboard
CGSize keyboardSize =
[[userInfo objectForKey :UIKeyboardFrameBeginUserInfoKey]
CGRectValue ] .size ;
// resize the scrollview
CGRect viewFrame = self .scrollView .frame ;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView
// was offset by the UITabBar so really only the portion of the keyboard that
// is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame .size .height += (keyboardSize .height - kTabBarHeight);
[ UIView beginAnimations :nil context : NULL ];
[ UIView setAnimationBeginsFromCurrentState : YES ];
// The kKeyboardAnimationDuration I am using is 0.3
[ UIView setAnimationDuration :kKeyboardAnimationDuration];
[ self .scrollView setFrame :viewFrame];
[ UIView commitAnimations ];
keyboardIsShown = NO ;
}
- ( void )keyboardWillShow:( NSNotification *)n
{
// This is an ivar I'm using to ensure that we do not do the frame size
// adjustment on the UIScrollView if the keyboard is already shown.
// This can happen if the user, after fixing editing a UITextField,
// scrolls the resized UIScrollView to another UITextField and attempts
// to edit the next UITextField. If we were to resize the UIScrollView again,
// it would be disastrous. NOTE: The keyboard notification will fire
// even when the keyboard is already shown.
if (keyboardIsShown) {
return ;
}
NSDictionary* userInfo = [n userInfo ];
// get the size of the keyboard
CGSize keyboardSize =
[[userInfo objectForKey :UIKeyboardFrameBeginUserInfoKey]
CGRectValue ] .size ;
// resize the noteView
CGRect viewFrame = self .scrollView .frame ;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView
// was offset by the UITabBar so really only the portion of the keyboard
// that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame .size .height -= (keyboardSize .height - kTabBarHeight);
[ UIView beginAnimations :nil context : NULL ];
[ UIView setAnimationBeginsFromCurrentState : YES ];
// The kKeyboardAnimationDuration I am using is 0.3
[ UIView setAnimationDuration :kKeyboardAnimationDuration];
[ self .scrollView setFrame :viewFrame];
[ UIView commitAnimations ];
keyboardIsShown = YES ;
}
Tag : keyboard , UIScrollView , 键盘
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值