使用滚动视图时将对象保持在原位
Keep objects in place when scroll view is used
我几天前发布了这个,但似乎找不到有效的解决方案。
(1)这是画面的整体布局。该按钮位于滚动视图内,因此当出现该按钮时,该按钮将被推到用户视图中。然后在第一响应者辞职时退缩。我根本不希望文本字段移动。我想将它们保存在我放置它们的位置。只有按钮是我要移动的。
(1) https://i.stack.imgur.com/O8iJy.png
(2)键盘出现时的样子结果。它最终将文本字段推出视图。
(2) https://i.stack.imgur.com/VDhdI.png
这是我正在使用的代码:
@IBOutlet weak var ScrollView: UIScrollView!
@IBOutlet weak var TextField: UITextField!
@IBOutlet weak var testButton: UIButton!
@IBOutlet weak var testView: UIView!
func closeKeyboard(){
self.view.endEditing(true)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
closeKeyboard()
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(scrollviewtest.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(scrollviewtest.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
而不是
self.view.frame.origin.y -= keyboardSize.height
在
keyboardWillShow
您必须为按钮底部创建 NSLayoutConstraint 以滚动视图底部,然后使用此约束进行操作。代码将如下所示。
@IBOutlet weak var buttonBottomConstraint: NSLayoutConstraint!
....
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.buttonBottomConstraint.constant -= keyboardSize.height
self.view.layoutIfNeeded()
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.buttonBottomConstraint.constant += keyboardSize.height
}
}
}
约束看起来像这样:
我几天前发布了这个,但似乎找不到有效的解决方案。
(1)这是画面的整体布局。该按钮位于滚动视图内,因此当出现该按钮时,该按钮将被推到用户视图中。然后在第一响应者辞职时退缩。我根本不希望文本字段移动。我想将它们保存在我放置它们的位置。只有按钮是我要移动的。
(1) https://i.stack.imgur.com/O8iJy.png
(2)键盘出现时的样子结果。它最终将文本字段推出视图。
(2) https://i.stack.imgur.com/VDhdI.png
这是我正在使用的代码: @IBOutlet weak var ScrollView: UIScrollView!
@IBOutlet weak var TextField: UITextField!
@IBOutlet weak var testButton: UIButton!
@IBOutlet weak var testView: UIView!
func closeKeyboard(){
self.view.endEditing(true)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
closeKeyboard()
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(scrollviewtest.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(scrollviewtest.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
而不是
self.view.frame.origin.y -= keyboardSize.height
在
keyboardWillShow
您必须为按钮底部创建 NSLayoutConstraint 以滚动视图底部,然后使用此约束进行操作。代码将如下所示。
@IBOutlet weak var buttonBottomConstraint: NSLayoutConstraint!
....
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.buttonBottomConstraint.constant -= keyboardSize.height
self.view.layoutIfNeeded()
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.buttonBottomConstraint.constant += keyboardSize.height
}
}
}
约束看起来像这样: