2013-02-20 81 views
0

我已經把它放在我的脖子上,試圖讓用戶在各個對象上看起來無縫的時候將整個視圖移動到合適的UITextField。我知道我並不是唯一一個絕對不喜歡這樣做的人。鍵盤出現時滾動視圖:庫或DIY?

什麼是最好的方法使盡可能少的工作儘可能美麗的工作?我試過TPKeyboardAvoiding,它完全吸了。

現在,我已經有了這個代碼去,但它在它自己的特殊方式吸收以及:

- (void)viewDidLoad 
{ 
    self.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin); 
    self.scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin); 

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

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


- (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); 
    self.scrollView.contentInset = contentInsets; 
    self.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, self.activeField.frame.origin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height); 
     [self.scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

- (void)keyboardWillBeHidden:(NSNotification *)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
} 
+0

你願意分享特別的方式嗎? – borrrden 2013-02-20 03:32:57

+0

當你最初點擊一個文本框(有很多)時,它很好用,但是當你點擊鍵盤上的返回按鈕時,它不會滾動到下一個「UITextField」。 – jakenberg 2013-02-20 03:37:12

+0

擊中返回從不滾動到下一個文本框。只有在移動Safari中激活表單時纔會發生這種情況。你必須爲此實現你自己的按鈕。編輯:這甚至沒有發生在瀏覽器中。你在鍵盤上得到了一個特殊的工具欄來做到這一點... – Ron 2013-02-20 03:46:56

回答

1

對我來說,它的工作原理是TPKeyboardAvoiding,我在我所有的項目中都使用它。您是否嘗試過:

  1. 添加一個UIScrollView到您的視圖控制器的XIB
  2. 的滾動視圖類爲TPKeyboardAvoidingScrollView(仍 在廈門國際銀行,通過身份檢查)
  3. 將所有的控制之內滾動視圖?

我也覺得這個解決方案:Keyboard Manager

  1. 下載演示項目
  2. 只需拖放KeyboardManager和SegmenedNextPrevious類項目
  3. 在你的appDelegate只寫一行代碼:

    [KeyBoardManager installKeyboardManager];

祝你好運!

+0

很久以前問過;我從那以後發現了'TPKeyboardAvoiding' :) – jakenberg 2014-02-12 21:46:57

0

你知道,如果你的視圖控制器從UITableViewController中派生,滾動表視圖當文本字段或文本視圖獲得焦點時鍵盤顯示時是否自動?你不必做任何事情。這就是說,它可能不適合你的應用程序重構UI,以便它使用帶有靜態單元的表格視圖,而不是你現在正在做的任何事情。

如果你這不會工作,你可以改變你的滾動視圖contenSize是當顯示鍵盤的可視面積的大小,然後調用scrollRectToVisible:animated:上滾動視圖通過它您keyboardWasShown內的文本框的矩形:選擇器。

+0

是的,我知道這一點,但你已經猜到了爲什麼我沒有使用'UITableViewController'。我剛剛完成了這一工作,並且我將在github上建立自己的圖書館,因爲所有其他人似乎都沒有完成這項工作。 – jakenberg 2013-02-20 04:52:30

相關問題