2013-04-11 70 views
0

我提出一個應用程序與多個UITextFieldUITextViewUITextView是在屏幕的底部,只要輸入開始,鍵盤塊UITextView鍵盤塊銀幕當鍵入

我如何能夠動起來當鍵盤出現在屏幕上時窗體的視圖?然後當鍵盤消失時再將其移下來?

+0

是的,你必須在顯示鍵盤滾動屏幕了。 – 2013-04-11 02:20:38

+3

你真的無法找到*任何*任何*任何*堆棧溢出或任何* * *的答案*? – matt 2013-04-11 02:20:51

+0

http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – matt 2013-04-11 02:21:38

回答

1

最好的答案是儘量避免這樣做。

但是,如果你把你的東西,在一個UIScrollView或一個UITableView可以滾動到輸入時,它成爲第一個響應者。

0

確保你在你的類實現UITextFieldDelegate。這些委託方法應該命名activeField的文本字段做的伎倆:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [activeField setDelegate:self];  
    [self configureView]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return TRUE; 
} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your application might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); 
     [scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 

- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

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