2014-09-29 84 views
11

我有一個scrollView,當顯示鍵盤時我想向上滾動。Swift:當鍵盤顯示時向上滾動視圖

我有此錯誤崩潰當鍵盤顯示:

2014年9月29日14:48:50.738 SWRD [1563:472888] - [swrd.EditPhotoViewController keyboardWasShown]:無法識別的選擇發送到實例0x14ed36640

這裏是我的代碼,有什麼不對?

func registerForKeyboardNotifications()-> Void { 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown", name: UIKeyboardDidShowNotification, object: nil) 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden", name: UIKeyboardWillHideNotification, object: nil) 


} 

func deregisterFromKeyboardNotifications() -> Void { 
    let center: NSNotificationCenter = NSNotificationCenter.defaultCenter() 
    center.removeObserver(self, name: UIKeyboardDidHideNotification, object: nil) 
    center.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) 


} 


func keyboardWasShown (notification: NSNotification) { 

    let info : NSDictionary = notification.userInfo! 
    let keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.frame 

    let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, keyboardSize!.height, 0) 

    self.scrollView.contentInset = insets 
    self.scrollView.scrollIndicatorInsets = insets 

    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y + keyboardSize!.height) 

} 

func keyboardWillBeHidden (notification: NSNotification) { 

    let info : NSDictionary = notification.userInfo! 
    let keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.frame 

    let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, keyboardSize!.height, 0) 

    self.scrollView.contentInset = insets 
    self.scrollView.scrollIndicatorInsets = insets 



} 


override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(true) 



    self.registerForKeyboardNotifications() 

} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(true) 

    self.deregisterFromKeyboardNotifications() 

} 

回答

10

在你的代碼,keyboardWasShownkeyboardWasHidden各帶參數的NSNotification。您需要用冒號終止addObserver中的選擇器,以便通過。即,keyboardWasShownkeyboardWasShown:是不同的選擇器。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil) 
10

見下一個獨立的解決方案:

private func startObservingKeyboardEvents() { 
    NSNotificationCenter.defaultCenter().addObserver(self, 
    selector:Selector("keyboardWillShow:"), 
    name:UIKeyboardWillShowNotification, 
    object:nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, 
    selector:Selector("keyboardWillHide:"), 
    name:UIKeyboardWillHideNotification, 
    object:nil) 
} 

private func stopObservingKeyboardEvents() { 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) 
} 

func keyboardWillShow(notification: NSNotification) { 
    if let userInfo = notification.userInfo { 
    if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size { 
     let contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0); 
    } 
    } 
} 

func keyboardWillHide(notification: NSNotification) { 
    let contentInset = UIEdgeInsetsZero; 
} 

使用contentInset變量調整內容插圖。

+1

感謝您包括所有的代碼,好東西,謝謝! – 2016-09-13 22:54:46

2

對我來說,是需要在代碼上面的一些變化:

1 - 首先,你需要把滾動視圖在視圖中,並設置約束條件是這樣的:

enter image description here

這一點很重要你的scrollView比視圖更粗糙。

2 - 放置您的TextField和您需要的其他組件。

3 - 鏈接滾動型到ViewController與@IBOutlet

4 - 代理添加到滾動型:

class ViewController: UIViewController, UITextFieldDelegate,  UIScrollViewDelegate { 
... 

5 - 添加觀察者:

override func viewWillAppear(animated: Bool) { 
    self.startKeyboardObserver() 
} 

override func viewWillDisappear(animated: Bool) { 
    self.stopKeyboardObserver() 
} 


private func startKeyboardObserver(){ 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) //WillShow and not Did ;) The View will run animated and smooth 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) 
} 

private func stopKeyboardObserver() { 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) 
} 

6 - 添加滾動代碼,計算KeyboardSize。

func keyboardWillShow(notification: NSNotification) { 
     if let userInfo = notification.userInfo { 
     if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size { 
      let contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0); 

     self.scrollView.contentInset = contentInset 
     self.scrollView.scrollIndicatorInsets = contentInset 

     self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0 + keyboardSize.height) //set zero instead self.scrollView.contentOffset.y 

     } 
    } 
} 

func keyboardWillHide(notification: NSNotification) { 
    if let userInfo = notification.userInfo { 
     if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size { 
      let contentInset = UIEdgeInsetsZero; 

     self.scrollView.contentInset = contentInset 
     self.scrollView.scrollIndicatorInsets = contentInset 
     self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y) 
     } 
    } 
} 
+0

出現鍵盤時會出現閃爍效果。第一個視圖向上移動,然後它會來到挖掘的領域 – 2015-11-26 19:30:59

相關問題