2013-05-08 73 views
0

我有一個視圖,就像你可以在這裏看到的一樣。 enter image description here向上移動一個視圖,使textfield不會消失在鍵盤後面

你可以看到,我有一個標題圖像下面有很多文本框。只有UITextfields Adres - 郵政編碼 - Gemeente - Tel.nr - 電子郵件 - BTWnr。正在鍵盤後方消失。 我有一些代碼,這是爲Adres的UITextfield工作。但是當我想要實現更多時,它總是需要Adres的UITextfield的動畫。

以下是我在代碼

#define kOFFSET_FOR_KEYBOARD 80.0 
#define kOFFSET_FOR_KEYBOARD2 120.0 
-(void)keyboardWillShow { 

    NSLog(@"Keyboard frame now is %f",self.keyboardView.frame.origin.y); 
    // Animate the current view out of the way 
    if (self.keyboardView.frame.origin.y >= 198) 
    { 
     NSLog(@"keyboardWillShow 1"); 
     [self setViewMovedUp2:NO]; 
    } 
    else if (self.keyboardView.frame.origin.y < 198) 
    { 
     NSLog(@"keyboardWillShow 2"); 
     [self setViewMovedUp:YES]; 
    } 
} 

-(void)keyboardWillHide { 

    NSLog(@"Keyboard frame ATM %f",self.keyboardView.frame.origin.y); 
    if (self.keyboardView.frame.origin.y >= 198) 
    { 
     NSLog(@"keyboardWillHide 1"); 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.keyboardView.frame.origin.y < 198) 
    { 
     NSLog(@"keyboardWillHide 2"); 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 

    if ([sender isEqual:txtAdres]) 
    { 
     NSLog(@"sender is adres"); 
     //move the main view, so that the keyboard does not hide it. 
     if (self.keyboardView.frame.origin.y >= 198) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
    if ([sender isEqual:txtPostcode]) 
    { 
     NSLog(@"sender is postcode"); 
     //move the main view, so that the keyboard does not hide it. 
     if (self.keyboardView.frame.origin.y >= 198) 
     { 
      [self setViewMovedUp2:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.keyboardView.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.keyboardView.frame = rect; 

    [UIView commitAnimations]; 
} 
-(void)setViewMovedUp2:(BOOL)movedUp 
{ 
    NSLog(@"setViewMovedUp2 called"); 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.keyboardView.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD2; 
     rect.size.height += kOFFSET_FOR_KEYBOARD2; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD2; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD2; 
    } 
    self.keyboardView.frame = rect; 

    [UIView commitAnimations]; 
} 

而在我的viewWillAppear中我做這個

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHide) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 

任何人可以幫助我嗎?

回答

1

請嘗試使用這一個...我希望它可以幫助你。你可以把你的價值放在你想要的文本框的這一行。

CGFloat avaliableHeight = applicationFrame.size.height - 200; 


- (void)scrollViewToCenterOfScreen:(UIView *)theView 
{ 
    CGFloat viewCenterY = theView.center.y; 
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
    CGFloat avaliableHeight = applicationFrame.size.height - 200; 

    CGFloat y = viewCenterY - avaliableHeight/2.0f; 

    if (y < 0) 
    { 
     y = 0; 
    } 

    [scrollView setContentOffset:CGPointMake(0, y) animated:YES]; 
} 


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    [self scrollViewToCenterOfScreen:textField]; 
    return YES; 

} 
0

我建議,使用脫入替代庫TPKeyboardAvoiding負責處理所有的工作,爲移動的看法,當鍵盤顯示,其實現的一個非常簡單的方法是

0

其實我碰到這個問題來了昨天。這是我的代碼:

#define kOFFSET_FOR_KEYBOARD 80.0 
//This decides how many pixels to move the view 

-(void)keyboardWillShow 
{ 
    // Animate the current view out of the way 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide 
{ 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:doNotCover]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 

    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 


- (void)viewWillAppear:(BOOL)animated 
{ 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHide) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillShowNotification 
                object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillHideNotification 
                object:nil]; 
} 
相關問題