2017-01-30 55 views
0

我有滾動視圖視圖控制器內,我在故事板設置它以這種方式(帶自動版式):滾動視圖和鍵盤問題斯威夫特

Example

正如你可以看到我添加的所有對象最後一個視圖(稱爲'viewsotto')在滾動視圖中。 我的問題是: 這些對象中的一些是textfield,我希望當我點擊它和鍵盤顯示它可以在文本框下方,以便我可以看到我寫入的內容。 爲此,我做了這種方式:

NotificationCenter.default.addObserver(self, selector: #selector(userProfiloGiusto.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.addObserver(self, selector: #selector(userProfiloGiusto.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

func keyboardWillShow(notification: NSNotification) { 

     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
      if self.view.frame.origin.y == 0{ 
       self.view.frame.origin.y -= keyboardSize.height 
      } 
     } 

    } 

    func keyboardWillHide(notification: NSNotification) { 
     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
      if self.view.frame.origin.y != 0{ 
       self.view.frame.origin.y += keyboardSize.height 
      } 
     } 
    } 

,但它不工作。我究竟做錯了什麼?

+0

檢查無需編碼https://github.com/hackiftekhar/IQKeyboardManager – karthikeyan

+0

@karthikeyan是對的。它也容易實現。 – vaibby

+0

謝謝。太奇妙了!! –

回答

0

請嘗試以下代碼。 首先添加var activeField: UITextField?來檢查文本字段的可用性。 然後在視圖做負載調用這個函數:

override func viewDidLoad() { 
    super.viewDidLoad()self.registerForKeyboardNotifications() 

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard)) 
    view.addGestureRecognizer(tap) 
    tap.cancelsTouchesInView = false} 


func registerForKeyboardNotifications() 
{ 
    //Adding notifies on keyboard appearing 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ProfileReviewViewController.keyboardWasShown(_:)), name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ProfileReviewViewController.keyboardWillBeHidden(_:)), name: UIKeyboardWillHideNotification, object: nil) 
} 


func deregisterFromKeyboardNotifications() 
{ 
    //Removing notifies on keyboard appearing 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) 
} 
func keyboardWasShown(notification: NSNotification) 
{ 
    //Need to calculate keyboard exact size due to Apple suggestions 
    let info : NSDictionary = notification.userInfo! 
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size 
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0) 


    self.scrollView.contentInset = contentInsets 
    self.scrollView.scrollIndicatorInsets = contentInsets 
    var aRect : CGRect = self.view.frame 
    aRect.size.height -= keyboardSize!.height 
    if activeField != nil 
    { 
     if (CGRectContainsPoint(aRect, activeField!.frame.origin)) 
     { 
      scrollView.scrollRectToVisible(activeField!.frame, animated: true) 
     } 
    } 
} 

func keyboardWillBeHidden(notification: NSNotification) 
{ 
    self.scrollView.contentInset = UIEdgeInsetsZero 
} 
//MARK : Textfield delegate methods 
func textFieldDidBeginEditing(textField: UITextField) { 
    activeField = textField 
} 

func textFieldShouldReturn(textField: UITextField) -> Bool { 

    self.view.endEditing(true) 
    activeField = nil 
    return false 
} 
func textFieldShouldEndEditing(textField: UITextField) -> Bool { //delegate method 

    activeField = nil 
    return true 
} 
func dismissKeyboard() { 
    //Causes the view (or one of its embedded text fields) to resign the first responder status. 

    activeField = nil 
    view.endEditing(true) 
} 
0

最有可能使用的是要了解是否有重疊的讀數是不正確的。
這是一個常見問題,您正在面對它們的主視圖,而應該獲取文本框的框架,進行正確的轉換以查看鍵盤是否與其重疊並相應地調整滾動視圖的內容插入.z
這在Managing the keyboard文檔中有很好的解釋。
當鍵盤顯示:

func keyboardWasShown(notification: NSNotification) 
{ 
    let info : NSDictionary = notification.userInfo! 
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size! 
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.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 app might not need or want this behavior. 
    var aRect : CGRect = self.view.frame 
    aRect.size.height -= keyboardSize.height 
    if let actField = activeField 
    { 
     if (CGRectContainsPoint(aRect, actField.frame.origin)) 
     { 
      scrollView.scrollRectToVisible(actField.frame, animated: true) 
     } 
    } 
} 

重要的一點是,當他們要求查看高度,他們減去鍵盤的高度,他們檢查文本視圖的是,區域內。
這個計算可能會改變你的視圖層次結構,這是因爲框架是一個視圖尊重它的超級視圖的位置,因此如果你的矩形在另一個視圖內,你需要轉換它的框架來獲得它的框架與主觀相關。