键盘向上移动时如何固定 UIView 的位置?
How to fix position of UIView when keyboard moved up?
键盘上移时,如何固定UIView
与image
和label
的位置?
我编写了一个代码,在键盘显示时向上移动我的 textField
,但它移动了所有内容。
func textFieldDidEndEditing(_ textField: UITextField) {
moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: false)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
rasstoyanietextField.resignFirstResponder()
return true
}
func moveTextField(textField: UITextField, moveDistance: Int, up: Bool){
let moveDuration = 0.1
let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(moveDuration)
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
我该如何解决?
添加键盘观察者
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardDidShow),
name: NSNotification.Name.UIKeyboardDidShow,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardWillHide),
name:NSNotification.Name.UIKeyboardWillHide,
object: nil)
我认为最好在具有 textfeilds/textViews 的 viewControllers 中使用滚动视图,以便在显示时向上移动所有项目,但在这里您可以挂钩 textfeild 的底部约束或其超级视图并将其拖动为 IBOutlet 并执行此操作
@objc func handleKeyboardDidShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.textFeilfBottomCon.constant = -1 * keyboardSize.height
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
}
@objc func handleKeyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.textFeilfBottomCon.constant = 0
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
}
因为您移动了整个视图,而不是仅移动了文本视图。换行:
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
与:
textField.frame = textField.frame.offsetBy(dx: 0, dy: movement)
键盘上移时,如何固定UIView
与image
和label
的位置?
我编写了一个代码,在键盘显示时向上移动我的 textField
,但它移动了所有内容。
func textFieldDidEndEditing(_ textField: UITextField) {
moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: false)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
rasstoyanietextField.resignFirstResponder()
return true
}
func moveTextField(textField: UITextField, moveDistance: Int, up: Bool){
let moveDuration = 0.1
let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(moveDuration)
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
我该如何解决?
添加键盘观察者
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardDidShow),
name: NSNotification.Name.UIKeyboardDidShow,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardWillHide),
name:NSNotification.Name.UIKeyboardWillHide,
object: nil)
我认为最好在具有 textfeilds/textViews 的 viewControllers 中使用滚动视图,以便在显示时向上移动所有项目,但在这里您可以挂钩 textfeild 的底部约束或其超级视图并将其拖动为 IBOutlet 并执行此操作
@objc func handleKeyboardDidShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.textFeilfBottomCon.constant = -1 * keyboardSize.height
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
}
@objc func handleKeyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.textFeilfBottomCon.constant = 0
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
}
因为您移动了整个视图,而不是仅移动了文本视图。换行:
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
与:
textField.frame = textField.frame.offsetBy(dx: 0, dy: movement)