適配不同型號(hào)的鍵盤(pán)大小
@property (nonatomic, assign)CGSize kbSize;
viewDidLoad
[self registerForKeyboardNotifications];
各種方法
- (void)registerForKeyboardNotifications{
//使用NSNotificationCenter 鍵盤(pán)出現(xiàn)時(shí)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)notify
{
NSDictionary* info = [notify userInfo];
//kbSize鍵盤(pán)尺寸 (有width, height)
self.kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//鍵盤(pán)高度
NSLog(@"hight_hight:%f",self.kbSize.height);
CGRect currentFrame = self.view.frame;
CGFloat change =self.kbSize.height;
currentFrame.origin.y = currentFrame.origin.y - change ;
[UIView animateWithDuration:0.00000000001 animations:^{
self.view.frame = currentFrame;
}];
}
///鍵盤(pán)消失事件
- (void) keyboardWillHidden {
//視圖下沉恢復(fù)原狀
[UIView animateWithDuration:0.00000000001 animations:^{
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}];
[【你的TextFiled】 resignFirstResponder];
}
// 視圖將要消失時(shí),移除通知中心
-(void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}