单击文本框时,我使键盘向上移动,但是单击另一个文本框时,它使视图向上移动了更多。谢谢您的帮助。
func keyboardStates() {
NotificationCenter.default.addObserver(self, selector:
#selector(keyboardWillChange(notification:)), name:
UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector:
#selector(keyboardWillChange(notification:)), name:
UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillChange(notification: Notification){
guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
if notification.name == UIResponder.keyboardWillShowNotification {
view.frame.origin.y = -keyboardRect.height/3
} else {
view.frame.origin.y = 0
}
}
答案 0 :(得分:0)
尝试以下方法
@objc
func keyboardWillChange(_ notification: NSNotification) {
guard let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }
guard let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else { return }
guard let startingFrame = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
guard let endingFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
let deltaY = endingFrame.origin.y - startingFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
答案 1 :(得分:0)
<!-- Start of a jumbotron -->
<div id="LoginFormJumbo" class="container">
<div id="Jumbotronlogin" class="jumbotron">
<!-- Form starts here -->
<form class="form-group mx-auto" action="#" method="POST">
<!-- Cross Site Request Forgery Token -->
{% csrf_token %}
<!-- End Cross Site Request Forgery -->
<div id="formtest" class="container">
<label id="email_address_label">{{form.email.label_tag}}</label>
<br> <!-- This is an example of how to layout your input & label -->
{{form.email}} <!-- This is calling the input -->
<br> </br>
<label id="password_label">{{form.password.label_tag}}</label>
<br> <-- Another example here -->
{{form.password}} <!-- Calling another input in our form -->
<br>
<a id="forgot_pass" href="#"> <small>Forgot password?</small></a>
<br> <br> </br>
<input id="login_submit_btn" type="submit" name="" value="Enter">
</div>
{% for i in form %}
<div class="fieldWrapper">
{{i.errors}}
{% if i.help_text %}
<p class="help">{{i.help_text|safe}}</p>
{% endif %}
{% endfor %}
</div>
</form>
<!-- Form ends here -->
</div>
</div>
</div>
<!-- End Jumbotron -->
答案 2 :(得分:0)
在每个视图控制器的基础上做到这一点将是困难且耗时的,我建议将这个出色的库用于此类任务,它支持滚动视图,表视图和集合视图。
https://github.com/hackiftekhar/IQKeyboardManager
尝试一下,我保证您也会喜欢它。
关于, ish