我几天前发布了此消息,但似乎无法找到有效的解决方案。
(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
}
}