gpt4 book ai didi

ios - 我的 UIButton 在显示键盘时向上移动时不会触发按钮操作

转载 作者:可可西里 更新时间:2023-11-01 01:09:20 26 4
gpt4 key购买 nike

override func viewDidLoad() {
super.viewDidLoad()
self.submitBtn.translatesAutoresizingMaskIntoConstraints = true

emailTf.text = emailID

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
// Do any additional setup after loading the view.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(PMSignupViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
// Do any additional setup after loading the view.
}

@objc func dismissKeyboard()
{
view.endEditing(true)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: true)
}

@objc func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: false)

}
func moveButton(_ button: UIButton, moveDistance: Int, up: Bool) {
let moveDuration = 0.3
let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)

UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(moveDuration)
self.submitBtn.frame = self.submitBtn.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}

@IBAction func submitBtnAction(_ sender: Any) {

}



}

所以基本上我有一个 View ,顶部有一个文本字段,底部有一个按钮。当我的键盘打开时,按钮会随之向上移动。但是按钮在键盘顶部时不会触发操作方法。但是当键盘被关闭并且它回到屏幕底部时它工作正常。对此有任何意见吗?

最佳答案

你最好使用自动布局,因为 frame-layout 在更改时会出现所有问题,因此,钩住按钮的底部约束,如 IBOutlet 名称所示 buttonBotCon

 @objc func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
self.buttonBotCon.constant = -keyboardHeight
self.view.layoutIfNeeded()
}

@objc func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
self.buttonBotCon.constant = 0
self.view.layoutIfNeeded()

}

编辑:在此处查看完成相同事情的演示 buttonUpKeyboard

关于ios - 我的 UIButton 在显示键盘时向上移动时不会触发按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49026231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com