IOS中輸入框被軟鍵盤遮擋的解決辦法
實現方法:
1)將輸入框的代理設置為self
(在lb文件中將輸入框的delegate設置為File’s Owner 。或者使用代碼textField.delegate = self;
2)將輸入框所對應的ViewController.h設置實現了UITextFieldDelegate協議
在ViewController.m文件中實現UITextFieldDelegate的三個方法即可:
//開始編輯輸入框的時候,軟鍵盤出現,執行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//鍵盤高度216NSTimeInterval 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); } </pre>
方法很簡單吧?請注意一定不要忘記設置輸入框的代理delegate哦
本文由用戶 xg48 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!