这是我编写的代码,用于在启用键盘时向上移动按钮:
@IBOutlet var lastNameText: HoshiTextField!
@IBOutlet var firstNameText: HoshiTextField!
override func viewDidLoad() {
var fab = MDCFloatingButton()
fab.translatesAutoresizingMaskIntoConstraints = false
fab.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant:-30.0).isActive = true
fab.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-240.0).isActive = true
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:.UIKeyboardWillShow, object: nil )
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:.UIKeyboardWillHide, object: nil)
@objc func keyboardWillShow(notificaion: NSNotification) {
if let keyboardSize = (notificaion.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue{
//fab = UIButton(frame: CGRect(10+20, self.view.frame.size.height - keyboardSize.height - 40,80,30))
fab.frame = CGRect(x: 320, y: 350, width: 60, height: 60)
}
}
@objc func keyboardWillHide(notification: NSNotification ) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue{
fab.frame = CGRect(x: 320, y: 350, width: 65, height: 65)
}
}
请输入。
答案 0 :(得分:1)
首先在您的类中添加textfield委托方法,然后只需输入此代码并根据您的更改: -
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: -10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: -10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: -10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: -80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: -80, up: true)
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: 10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: 10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: 10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: 80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: 80, up: true)
default:
break
}
}
答案 1 :(得分:1)
已更新为 Swift 4。+
viewDidLoad
// Moving next button when keyboard appears
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name:UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name:UIResponder.keyboardWillHideNotification, object: nil)
显示键盘时:
@objc func keyboardWillShow(notification:NSNotification){
let userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
self.buttonNextBottomConstraint.constant = keyboardFrame.size.height
}
隐藏键盘时:
@objc func keyboardWillHide(notification:NSNotification){
self.buttonNextBottomConstraint.constant = 0
}
答案 2 :(得分:0)
您需要为按钮添加底部约束,并以编程方式更改此约束的常量。 首先,您需要将Observers添加到viewDidLoad()方法。
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
并添加选择器方法:
func keyboardWillShow(notification:NSNotification){
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
bottom.constant = keyboardFrame.size.height
}
func keyboardWillHide(notification:NSNotification){
bottom.constant = 0
}