保持iOS上鍵盤出現時輸入框不被覆蓋

來源:互聯網
上載者:User

標籤:style   io   ar   os   sp   for   檔案   on   cti   

 

如果螢幕中的內容項目比較多,鍵盤就可能覆蓋住文本輸入框之類的對象。你必須調整你的內容,使得輸入框保持可見。

你會想到哪些處理方法呢?

第一種,

臨時調整視窗中各個視圖的大小,使得鍵盤從下向上佔領的地區空白。鍵盤的高度( keyboard.size.height )是一定的,將視圖中所有內容所在地區的 y 值減小到 y-keyboard.size.height 。

該方法有個局限,如果所有內容之和大於視窗減去鍵盤高度的話,該方法將不能用。

第二種,

將視窗中所有視圖嵌入進一個滾動視圖對象( UIScrollView )中。在鍵盤出現時,你將輸入框滾動到合適的位置,調整一下滾動視圖的內容地區。

這些操作通過一個通知 UIKeyboardDidShowNotification   去實現的,邏輯過程如下:

1 、根據通知的字典資訊 userInfo 得到鍵盤的 size 。

2 、根據鍵盤的 size 中的 height 值,調整滾動視圖內容底部的 inset 。

3 、滾動目標視圖即檔案輸入框進入視圖中。

簡要的代碼如下:

1 、實現兩個委託方法,用於指定輸入框對象。

-   (void)textFieldDidBeginEditing:(UITextField *)textField

{

      activeField = textField;

}

 

- (void)textFieldDidEndEditing:(UITextField  *)textField

{

      activeField = nil;

}

2 、註冊通知的觀察者

- (void)registerForKeyboardNotifications

{

      [[NSNotificationCenter defaultCenter] addObserver:self

              selector:@selector(keyboardWasShown:)

              name:UIKeyboardDidShowNotification object:nil];

 

     [[NSNotificationCenter defaultCenter] addObserver:self

               selector:@selector(keyboardWillBeHidden:)

               name:UIKeyboardWillHideNotification object:nil];

 

}

將這個方法放在 viewDidAppear 中調用。

同時也要寫一個 removeObserver 放在 viewWillDisappear 中調用。

3 、實現鍵盤顯示通知的 selector 中的方法

 

// 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];

    }

}

 

4 、實現鍵盤消失通知的方法

 

// Called when the  UIKeyboardWillHideNotification is sent

-  (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

      UIEdgeInsets contentInsets = UIEdgeInsetsZero;

      scrollView.contentInset = contentInsets;

    scrollView.scrollIndicatorInsets  = contentInsets;

}

 

這個方法調整內容底部的 inset 的值使得輸入框不被鍵盤地區屏蔽的。還可以換種方法實現。

 

第三種,

擴充內容視圖的高度,滾動文本輸入框對象進內容視圖。

將 keyboardWasShown: 重寫。

 

-   (void)keyboardWasShown:(NSNotification*)aNotification {

    NSDictionary*  info = [aNotification userInfo];

      CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]  CGRectValue].size;

      CGRect bkgndRect = activeField.superview.frame;

      bkgndRect.size.height += kbSize.height;

    [activeField.superview  setFrame:bkgndRect];

      [scrollView setContentOffset:CGPointMake(0.0,  activeField.frame.origin.y-kbSize.height) animated:YES];

}

保持iOS上鍵盤出現時輸入框不被覆蓋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.