2016年3月16日 星期三

UITextField 擋住畫面怎麼辦

這邊的處理是,當鍵盤彈出,檔到輸入框時,
我把畫面往上推,當鍵盤縮下去,再把畫面推回來!

p.s當初面試一間公司,考官就有考我這一題

下面是最重要的:

 1 -(void)moveView:(float)move{
 2     NSTimeInterval animationDuration = 0.30f;  
 3     CGRect frame = self.view.frame;    //先抓到目前的frame
 4     frame.origin.y +=move;   //要往上或下推,就是y
 5     self.view.frame = frame;
 6     
 7     [UIView beginAnimations:@"設個字串名字" context:nil];
 8     [UIView setAnimationDuration:animationDuration];
 9     self.view.frame = frame;
10     [UIView commitAnimations];
11     
12     
13 }

這邊就是看你有幾個輸入框,自己試試看,我的tag設置是從下到上,可以自己試試看推動多少ok,


 1 - (void)textFieldDidBeginEditing:(UITextField *)textField {
 2     
 3     if (textField.tag==1) {
 4         [self moveView:-100];
 5     }
 6     if (textField.tag==2)
 7     {
 8         [self moveView:-60];
 9     }
10     if (textField.tag==3)
11     {
12         [self moveView:-60];
13     }
14     if (textField.tag==4)
15     {
16         [self moveView:-60];
17     }
18 }
19 
20 - (void)textFieldDidEndEditing:(UITextField *)textField {
21     if (textField.tag==1) {
22         [self moveView:100];
23     }
24     if (textField.tag==2)
25     {
26         [self moveView:60];
27     }
28     if (textField.tag==3)
29     {
30         [self moveView:60];
31     }
32     if (textField.tag==4)
33     {
34         [self moveView:60];
35     }
36 }

希望對您有用

沒有留言:

張貼留言