在iOS開發過程當中,遇到關於鍵盤遮擋輸入框的問題,經過網絡參考與實踐,總結如下:
登錄窗口,上下放置兩個UITextField,一個用戶名,一個密碼,放置的在屏幕下方1/3處,當點擊用戶名時,自動彈出鍵盤,正好擋住瞭輸入框
解決思路:
1、BLoginViewController 實現UITextViewDelegate的方法
復制代碼
1 //實現瞭UITextFieldDelegate中的方法,當對TextField進行編輯即鍵盤彈出時,自動將輸入框上移
2 -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
3 NSTimeInterval animationDuration=0.30f;
4 [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
5 [UIView setAnimationDuration:animationDuration];
6 float width = self.view.frame.size.width;
7 float height = self.view.frame.size.height;
8 //上移n個單位,按實際情況設置
9 CGRect rect=CGRectMake(0.0f,-130,width,height);
10 self.view.frame=rect;
11 [UIView commitAnimations];
12 return YES;
13 }
復制代碼
2、為輸入框設置代理
復制代碼
1 – (void)viewDidLoad
2 {
3 [super viewDidLoad];
4
5 //狀態欄白色字體
6 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
7
8 UIColor *btnBgColor = [UIColor colorWithWhite:1.0f alpha:1.0];
9 [_buttonLogin setBackgroundColor:btnBgColor];
10
11 //為輸入框添加代理
12 _textFieldUserName.delegate = self;
13 _textFieldPassword.delegate = self;
14
15 }