在iOS的屏幕上显示键盘时,自动布局和将视图向上移动时遇到了一些麻烦,因为它被掩盖了。在线查看有多种解决方案,但最常见的是使用通知中心来获取键盘的大小并为视图添加动画效果。我已经创建了IBOutlet来更改约束,但是我一直收到错误消息“ UIView”类型的值没有成员“ passwordBottom”。
任何人都可以帮助我解决问题吗?
class LoginViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBOutlet weak var usernameBottom: NSLayoutConstraint!
@IBOutlet weak var passwordBottom: NSLayoutConstraint!
@IBOutlet weak var usernameTF: UITextField!
@IBOutlet weak var passwordTF: UITextField!
var offsetY:CGFloat = 0
@IBAction func signinPressed(_ sender: Any) {
}
@IBAction func forgotPasswordPressed(_ sender: Any) {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil )
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc func keyboardWillShow( notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let newHeight: CGFloat
let duration:TimeInterval = (notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
if #available(iOS 11.0, *) {
newHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom
} else {
newHeight = keyboardFrame.cgRectValue.height
}
let keyboardHeight = newHeight + 10
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: {
self.view.passwordBottom.constant = keyboardHeight
self.view.layoutIfNeeded() },
completion: nil)
}
}
}
答案 0 :(得分:0)
passwordBottom
是VC中的属性,而不是视图中的属性。
更改:
self.view.passwordBottom.constant = keyboardHeight
变成:
self.passwordBottom.constant = keyboardHeight