启用键盘时向上移动按钮
Moving button up when keyboard is enabled
这是我编写的用于在启用键盘时向上移动按钮的代码:
@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)
}
}
请提供意见。
您需要为按钮添加底部约束,并以编程方式更改此约束的常量。
首先,您需要将 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
}
首先在您的 class 中添加文本字段委托方法,然后简单地放置此代码并根据您的需要进行更改:-
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
}
}
更新为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
}
这是我编写的用于在启用键盘时向上移动按钮的代码:
@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)
}
}
请提供意见。
您需要为按钮添加底部约束,并以编程方式更改此约束的常量。 首先,您需要将 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
}
首先在您的 class 中添加文本字段委托方法,然后简单地放置此代码并根据您的需要进行更改:-
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
}
}
更新为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
}