[英]Keyboard hides UITextView when it appears
我已經把我的UIkeyboarddismissmode
是.Interactive
,我不知道該如何調整UITextView
使鍵盤不會涵蓋的內容。 我還有一個UIToolbar
作為inputaccessory
。
這是我的代碼。
@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
以一種奇怪的方式移動,我不知道如何實現像Message應用程序那樣的效果。
更新:
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會突然彈到底部,這非常奇怪。 有什么建議嗎?
UITextView是一個滾動視圖。 當鍵盤出現時你不需要調整它的大小:你應該調整它的contentInset屬性,將.bottom增加鍵盤的高度。
無論您是否有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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.