Swift:显示键盘时更改自动布局约束
Swift: Change autolayout constraints when keyboard is shown
我试图让 UItextView
的底部在出现键盘时更改约束,因此 UItextView
是 textView 的 "constrained"。我有一个 UIView
我在 UItextView
和底部布局指南之间命名了 spacer。我正在尝试将视图底部的垫片约束值更改为键盘高度。感谢任何帮助。
这是我的代码:
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var spacer: UIView!
@IBOutlet weak var spacerBottomLayoutConstraint: NSLayoutConstraint!
在 viewDidLoad
中:
textView.becomeFirstResponder()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AdditionalDetailsVC.keyboardShown(_:)), name: UIKeyboardDidShowNotification, object: nil)
奇迹发生的地方:
func keyboardShown(notification: NSNotification) {
let info = notification.userInfo!
let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]!
let rawFrame = value.CGRectValue
keyboardFrame = view.convertRect(rawFrame, fromView: nil)
print(keyboardFrame)
print(spacerBottomLayoutConstraint)
//Setting text field to bottom of keyboard
//spacerBottomLayoutConstraint.constant = keyboardFrame.height
UIView.animateWithDuration(1, animations: {
self.spacerBottomLayoutConstraint.constant = self.keyboardFrame.height
self.view.layoutIfNeeded()
})
}
我个人觉得调整文本视图的底部内容插入要简单得多。此代码用于占据整个 window 的文本视图(因此它的底部是屏幕底部);一个没有下降到那么远的文本视图将需要更多的基本算术:
func keyboardShown(n:NSNotification) {
let d = n.userInfo!
var r = (d[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
r = self.textView.convertRect(r, fromView:nil)
self.textView.contentInset.bottom = r.size.height
self.textView.scrollIndicatorInsets.bottom = r.size.height
}
您可以尝试使用以下代码将视图底部的间隔器的约束值更改为键盘高度。将以下代码放入您的 ViewController.
// MARK: Keyboard Event Notifications
func handleKeyboardNotification(notification: NSNotification) {
let userInfo = notification.userInfo!
print(notification)
print(notification.object)
// Get information about the animation.
let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let rawAnimationCurveValue = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).unsignedLongValue
let animationCurve = UIViewAnimationOptions(rawValue: rawAnimationCurveValue)
// Convert the keyboard frame from screen to view coordinates.
let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardViewBeginFrame = view.convertRect(keyboardScreenBeginFrame, fromView: view.window)
let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
print(keyboardViewBeginFrame)
print(keyboardViewEndFrame)
// Determine how far the keyboard has moved up or down.
let originDelta = keyboardViewEndFrame.origin.y - keyboardViewBeginFrame.origin.y
print(originDelta)
// Adjust the table view's scroll indicator and content insets.
textView.scrollIndicatorInsets.bottom -= originDelta
textView.contentInset.bottom -= originDelta
print(keyboardViewEndFrame)
spacerBottomLayoutConstraint?.constant = CGFloat(originDelta)
// Inform the view that its the layout should be updated.
[self.view setNeedsLayout];
// Animate updating the view's layout by calling layoutIfNeeded inside a UIView animation block.
let animationOptions: UIViewAnimationOptions = [animationCurve, .BeginFromCurrentState]
UIView.animateWithDuration(animationDuration, delay: 0, options: animationOptions, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
我试图让 UItextView
的底部在出现键盘时更改约束,因此 UItextView
是 textView 的 "constrained"。我有一个 UIView
我在 UItextView
和底部布局指南之间命名了 spacer。我正在尝试将视图底部的垫片约束值更改为键盘高度。感谢任何帮助。
这是我的代码:
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var spacer: UIView!
@IBOutlet weak var spacerBottomLayoutConstraint: NSLayoutConstraint!
在 viewDidLoad
中:
textView.becomeFirstResponder()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AdditionalDetailsVC.keyboardShown(_:)), name: UIKeyboardDidShowNotification, object: nil)
奇迹发生的地方:
func keyboardShown(notification: NSNotification) {
let info = notification.userInfo!
let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]!
let rawFrame = value.CGRectValue
keyboardFrame = view.convertRect(rawFrame, fromView: nil)
print(keyboardFrame)
print(spacerBottomLayoutConstraint)
//Setting text field to bottom of keyboard
//spacerBottomLayoutConstraint.constant = keyboardFrame.height
UIView.animateWithDuration(1, animations: {
self.spacerBottomLayoutConstraint.constant = self.keyboardFrame.height
self.view.layoutIfNeeded()
})
}
我个人觉得调整文本视图的底部内容插入要简单得多。此代码用于占据整个 window 的文本视图(因此它的底部是屏幕底部);一个没有下降到那么远的文本视图将需要更多的基本算术:
func keyboardShown(n:NSNotification) {
let d = n.userInfo!
var r = (d[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
r = self.textView.convertRect(r, fromView:nil)
self.textView.contentInset.bottom = r.size.height
self.textView.scrollIndicatorInsets.bottom = r.size.height
}
您可以尝试使用以下代码将视图底部的间隔器的约束值更改为键盘高度。将以下代码放入您的 ViewController.
// MARK: Keyboard Event Notifications
func handleKeyboardNotification(notification: NSNotification) {
let userInfo = notification.userInfo!
print(notification)
print(notification.object)
// Get information about the animation.
let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let rawAnimationCurveValue = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).unsignedLongValue
let animationCurve = UIViewAnimationOptions(rawValue: rawAnimationCurveValue)
// Convert the keyboard frame from screen to view coordinates.
let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardViewBeginFrame = view.convertRect(keyboardScreenBeginFrame, fromView: view.window)
let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
print(keyboardViewBeginFrame)
print(keyboardViewEndFrame)
// Determine how far the keyboard has moved up or down.
let originDelta = keyboardViewEndFrame.origin.y - keyboardViewBeginFrame.origin.y
print(originDelta)
// Adjust the table view's scroll indicator and content insets.
textView.scrollIndicatorInsets.bottom -= originDelta
textView.contentInset.bottom -= originDelta
print(keyboardViewEndFrame)
spacerBottomLayoutConstraint?.constant = CGFloat(originDelta)
// Inform the view that its the layout should be updated.
[self.view setNeedsLayout];
// Animate updating the view's layout by calling layoutIfNeeded inside a UIView animation block.
let animationOptions: UIViewAnimationOptions = [animationCurve, .BeginFromCurrentState]
UIView.animateWithDuration(animationDuration, delay: 0, options: animationOptions, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}