- //開始編輯輸入框的時候,軟鍵盤出現,執行此事件
- -(void)textFieldDidBeginEditing:(UITextField *)textField
- {
- CGRect frame = textField.frame;
- int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//鍵盤高度216
- NSTimeInterval animationDuration = 0.30f;
- [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
- [UIView setAnimationDuration:animationDuration];
- //將視圖的Y座標向上移動offset個單位,以使下面騰出地方用於軟鍵盤的顯示
- if(offset > 0)
- self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
- [UIView commitAnimations];
- }
- //當用戶按下return鍵或者按回車鍵,keyboard消失
- -(BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- [textField resignFirstResponder];
- return YES;
- }
- //輸入框編輯完成以後,將視圖恢復到原始狀態
- -(void)textFieldDidEndEditing:(UITextField *)textField
- {
- self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
- }
iOS UITextfield 鍵盤遮擋解決代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.