界面随键盘顶起来
在你的代碼相關位置添加兩個事件
UIKeyboardWillShowNotification,這個是鍵盤即將彈出的事件通知名稱
UIKeyboardWillHideNotification,這個是鍵盤即將消失的事件通知名稱
在這兩個通知的回調方法中處理你的工具條動畫就行了,鍵盤的相關參數會在NSNotification實例中的userInfo對象中傳過去(鍵盤的高度、寬度、鍵盤動畫的duration等),你可以在回調函數中根據這些參數處理你的動畫,讓工具條和鍵盤動畫達到同步的效果。
如果你的系統是ios5,那需要注意一點的就是,不同輸入法的鍵盤高度不同了(可能是216或252),你可能需要根據鍵盤的具體高度來指定工具條的高度,而不能統一寫死為216了。
同時,ios5里面還增加了一個事件通知名稱:
UIKeyboardWillChangeFrameNotification
這個通知會在鍵盤即將改變其大小的時候發出來(比如說鍵盤彈出、收起、用戶切換輸入法、用戶分享鍵盤時引起的鍵盤大小變化),如果你想要你的工具條嚴格保持在鍵盤頂部的時候,就需要監聽這個事件。
今天在ios5里面偶然發現了問題,補充一下。
UIKeyboardWillChangeFrameNotification
這個通知會在鍵盤即將改變其大小或位置的時候發出來,包括:
1:鍵盤彈出、收起。
2:用戶切換輸入法。
3:用戶分割鍵盤(iPad專有)
4:如果你在viewWillDisappear中讓鍵盤收起(比如說textField resignFirstResponder),鍵盤似乎不會收起,而只是會改變frame的orig值。
?
參考以下代碼(將self.view換成需要上移的view即可):
?
-(void)keyboardWillChangeFrame:(NSNotification *)notify{ //NSLog(@"=====%@,%@",notify.object,notify.userInfo); self.inputView__.backgroundColor=[UIColor groupTableViewBackgroundColor];NSDictionary *userInfo=notify.userInfo; NSString *keyboard_frame_begin_NSRectString= [userInfo[UIKeyboardFrameBeginUserInfoKey] description]; NSString *keyboard_frame_begin_CGRectString= nil; if ([keyboard_frame_begin_NSRectString hasPrefix:@"NSRect"]) {keyboard_frame_begin_CGRectString=[keyboard_frame_begin_NSRectString stringByReplacingOccurrencesOfString:@"NSRect"withString:@"CGRect" ];}else if( [keyboard_frame_begin_NSRectString hasPrefix:@"CGRect"]){keyboard_frame_begin_CGRectString=keyboard_frame_begin_NSRectString;} CGRect keyboard_frame_begin= CGRectFromString(keyboard_frame_begin_CGRectString);NSString *keyboard_frame_end_NSRectString= [userInfo[UIKeyboardFrameEndUserInfoKey] description]; NSString *keyboard_frame_end_CGRectString= nil; if ([keyboard_frame_end_NSRectString hasPrefix:@"NSRect"]) {keyboard_frame_end_CGRectString=[keyboard_frame_end_NSRectString stringByReplacingOccurrencesOfString:@"NSRect"withString:@"CGRect" ];}else if( [keyboard_frame_end_NSRectString hasPrefix:@"CGRect"]){keyboard_frame_end_CGRectString=keyboard_frame_end_NSRectString;} CGRect keyboard_frame_end= CGRectFromString(keyboard_frame_end_CGRectString);CGRect rect=self.view.frame;CGFloat y_keyboard_begin=keyboard_frame_begin.origin.y;CGFloat y_keyboard_end=keyboard_frame_end.origin.y;CGFloat y_current=CGRectGetMinY(rect); CGFloat y_keyboard_change=y_keyboard_end-y_keyboard_begin; CGFloat y= y_current +y_keyboard_change; //NSLog(@"y===========%f",y );[UIView animateWithDuration:0.0 animations:^{ self.view.frame=CGRectMake(0, y, CGRectGetWidth(rect), CGRectGetHeight(rect));}];}轉載于:https://www.cnblogs.com/LiuLady12138/p/4708915.html
總結
- 上一篇: git pull时冲突的几种解决方式
- 下一篇: HDU 4911