当键盘出现时,键盘会隐藏UITextView。
当键盘出现时,键盘会隐藏UITextView。
我将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)
// 添加键盘通知
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)
}
// 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()
// 更改contentInset
self.textView.contentInset.bottom = endFrame!.height
self.textView.scrollIndicatorInsets.bottom = endFrame!.height
}
}
我将代码更改为上述内容。但有时当我将键盘部分释放时,scrollView会突然跳到底部,这很奇怪。有什么建议吗?
键盘在出现时隐藏UITextView的原因是由于键盘遮挡了UITextView,导致其无法显示在屏幕上。为了解决这个问题,可以使用以下方法:
1. 首先,在故事板中设置一个垂直间距约束,用于将文本视图与父视图的底部对齐。将其命名为"bottomSpacingConstraint"。
2. 在视图控制器的"viewDidLoad"方法中,添加以下代码来接收键盘出现和隐藏的通知:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
3. 实现"keyboardNotification"方法,用于处理键盘出现和隐藏的逻辑。在该方法中,根据通知的类型判断键盘是出现还是隐藏,并获取键盘的高度和动画相关的信息。
@objc func keyboardNotification(_ notification: NSNotification) {
let isShowing = notification.name == UIResponder.keyboardWillShowNotification
var tabbarHeight: CGFloat = 0
if let tabBarController = self.tabBarController {
tabbarHeight = tabBarController.tabBar.frame.height
}
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration: TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve = UIView.AnimationOptions(rawValue: animationCurveRaw)
self.bottomSpacingConstraint.constant = isShowing ? (endFrame?.size.height ?? 0 - tabbarHeight) : self.originalConstraint
UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
通过以上步骤,当键盘出现时,文本视图将自动向上移动以避免被键盘遮挡。当键盘隐藏时,文本视图将恢复到原始位置。
参考链接:[Move textfield when keyboard appears swift](https://stackoverflow.com/questions/25693130/29488420#29488420)