我有底部文本字段,当我按下那里时,我看到键盘,看看我的滚动视图如何将其所有内容滚动到键盘高度。但是,如果我按“返回”,然后再次点击最后一个文本字段,我的键盘就出现在屏幕上,但滚动视图这次没有滚动,底部文本字段留在键盘下面。
这是我的代码:
func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc
func keyboardWasShown(_ notification: NSNotification) {
guard let info = notification.userInfo,
let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
let keyboardFrame = keyboardFrameValue.cgRectValue
let keyboardSize = keyboardFrame.size
print("keyboardSize: \(keyboardSize)")
let contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
print("keyboardSize.height: \(keyboardSize.height)")
scrollView.contentInset = contentInset
scrollView.scrollIndicatorInsets = contentInset
}
@objc
func keyboardWillBeHidden(_ notification: NSNotification) {
let contentInsets = UIEdgeInsets.zero
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
}
extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
}
这是输出:
有人可以解释为什么会这样吗?
谢谢。答案 0 :(得分:0)
你可以尝试自动布局方法,将IBOutlet
scrollBotcon
说@objc func keyboardWasShown(_ notification: NSNotification) {
guard let info = notification.userInfo,
let keyboardFrameValue = info[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = keyboardFrameValue.cgRectValue
let keyboardSize = keyboardFrame.size
self.scrollBotcon.constant = - keyboardSize.height
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
}
}
@objc func keyboardWillBeHidden(_ notification: NSNotification) {
self.scrollBotcon.constant = 0
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
}
}
<html>
<head>
<title>Today's Events at the TRU Union</title>
<style>
p {text-align: center;}
table {display: block; margin: auto;}
body img{display: block;margin-left:auto;margin-right:auto;}
h2 {text-align: center; color: red;}
</style>
</head>
<body background="Back01.jpg" text="black">
<div id="table"></div>
</body>
<script>
function determine_picture_day_and_month(){
var table_content = insert_table();
table_content += insert_title_rules_and_image();
return table_content;
}
function insert_table(){
var daily_images = ["Weekend.gif", "Monday.gif", "Tuesday.gif", "Wednesday.gif",
"Thursday.gif", "Friday.gif", "Weekend.gif"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var dateObject = new Date();
var current_image = daily_images[dateObject.getDay()];
var currentday = days[dateObject.getDay()];
var currentmonth = months[dateObject.getMonth()];
var table_content = "<table border='1' cellpadding='5'>"
table_content += "<tr><td width='100'>" + currentday +
"</td><td><img src='" + current_image + "' /></td></tr></table>";
table_content += "<p>Today is " + currentday + ", " + currentmonth +
" " + dateObject.getDate() + "</p>";
return table_content;
}
function insert_title_rules_and_image(){
var table_content = "<h2>TRU Student Union Daily Schedule</h2>";
table_content += "<hr /><img src='Events.gif' /><hr />";
return table_content;
}
document.getElementById("table").innerHTML = determine_picture_day_and_month();
</script>
</html>