2014-12-24 55 views
2

我已經將我的UIkeyboarddismissmode設置爲.Interactive,我不知道如何調整UITextView的大小,以便鍵盤不會覆蓋內容。我也有UIToolbar作爲inputaccessory鍵盤隱藏UITextView當它出現

這是我的代碼。

@IBOutlet weak var textView: UITextView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.addDoneButtonOnKeyboard() 

    self.textView.keyboardDismissMode = .Interactive 
} 

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

    // Add notification about keyboard 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillHideNotification, object: nil) 
    //  NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardFrameDidChange:"), name:UIKeyboardWillChangeFrameNotification, object: nil) 
} 

override func viewDidDisappear(animated: Bool) { 
    super.viewDidDisappear(true) 
    NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillShowNotification) 
    NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillHideNotification) 
} 


// The UIToolbar 

var toolbar = UIToolbar() 

func addDoneButtonOnKeyboard() 
{ 
    var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44)) 
    doneToolbar.barStyle = UIBarStyle.Default 

    var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    var photo: UIBarButtonItem = UIBarButtonItem(title: "Add Photo", style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("photoButtonAction")) 
    var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction")) 

    var items = NSMutableArray() 
    items.addObject(photo) 
    items.addObject(flexSpace) 
    items.addObject(done) 

    doneToolbar.items = items 
    doneToolbar.sizeToFit() 

    doneToolbar.tintColor = UIColor(red: 240/225, green: 42/225, blue: 20/225, alpha: 1) 
    doneToolbar.translucent = true 

    self.toolbar = doneToolbar 
    self.textView.inputAccessoryView = self.toolbar 
} 

func keyboardFrameDidChange(notification: NSNotification) { 
    if let userInfo = notification.userInfo { 
     var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() 
     var beginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() 

     let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber 
     let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue 
     let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw) 

     var animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 

     var newFrame = self.textView.frame 
     var keyboardFrameEnd = self.view.convertRect(endFrame!, toView: nil) 
     var keyboardFrameBegin = self.view.convertRect(beginFrame!, toView: nil) 

     newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y) 
     self.textView.frame = newFrame 

     UIView.animateWithDuration(animationDuration, 
      delay: NSTimeInterval(0), 
      options: animationCurve, 
      animations: { self.view.layoutIfNeeded() }, 
      completion: nil) 
    } 

} 

上面的代碼只能使以一種不可思議的方式textView舉動,我不知道如何實現,如消息應用程序的影響。

更新:

func keyboardFrameEnd(notification: NSNotification) { 
    if let userInfo = notification.userInfo { 
     var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() 

     // Change contentInset 
     self.textView.contentInset.bottom = endFrame!.height 
     self.textView.scrollIndicatorInsets.bottom = endFrame!.height 
    } 
} 

我改變了我的代碼上面。但有時當我釋放鍵盤時,scrollView會突然反彈到底部,這很奇怪。任何建議?

+0

嘗試此:[鏈接](http://stackoverflow.com/questions/11282449/move-uiview-up-when-the-keyboard-appears-in-ios)。我知道它是Objective-C的代碼,但你可以將它適應Swift。 – dehlen

+0

http://stackoverflow.com/questions/25693130/move-textfield-when-keyboard-appears-swift看看這個 – Sport

+0

我已經嘗試了上面的方法,但它們都是用動畫硬編碼的。我的keyboarddissmissmode設置爲交互式,這意味着您可以手動拖動鍵盤,而不是像iMessage應用那樣動畫。 –

回答

2

UITextView是一個滾動視圖。當鍵盤出現時,您不需要調整它的大小:您應該調整它的contentInset屬性,通過鍵盤的高度增加.bottom。

+0

謝謝。它部分達到了預期的效果。然而,右側的滾動條並沒有改變,這是令人困惑的。任何方法來解決這個問題? –

+0

解決了scrollIndicatorInsets.bottom的問題。謝謝! –

+0

這就是這樣的想法:滾動視圖在鍵盤後面延伸,插入僅添加在底部,這樣您可以向下滾動並查看文本的底部。 – Gregzo

0

它可以適用於你是否有tabbar或不。 它將完美還原通過鍵盤移動到原始位置的視圖。

// Set this up in storyboard first. 
// It's a vertical spacing constraint between your text view and bottom of superview. 
@IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint! 

override func viewDidLoad() { 
     super.viewDidLoad()    

     // Receive(Get) Notification 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillShowNotification, object: nil) 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillHideNotification, object: nil) 


     self.originalConstraint = self.keyboardHeightLayoutConstraint?.constant //for original coordinate. 
} 

func keyboardNotification(notification: NSNotification) { 
     let isShowing = notification.name == UIKeyboardWillShowNotification 

     var tabbarHeight: CGFloat = 0 
     if self.tabBarController? != nil { 
      tabbarHeight = self.tabBarController!.tabBar.frame.height 
     } 
     if let userInfo = notification.userInfo { 
      let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() 
      let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 
      let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber 
      let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue 
      let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw) 
      self.keyboardHeightLayoutConstraint?.constant = isShowing ? (endFrame!.size.height - tabbarHeight) : self.originalConstraint! 
      UIView.animateWithDuration(duration, 
       delay: NSTimeInterval(0), 
       options: animationCurve, 
       animations: { self.view.layoutIfNeeded() }, 
       completion: nil) 
     } 
} 

Move textfield when keyboard appears swift