Scrollview и клавиатура Swift
Я новичок здесь, и я начинаю с Swift для iOS.
Я начал создавать простое приложение, которое выполняет некоторые операции. Но у меня возникают проблемы, когда появляется клавиатура, скрывая один из моих текстовых полей. Я думаю, что это общая проблема, и я сделал некоторые исследования, но я не мог найти ничего, что могло бы решить мою проблему. И я хочу использовать Scroll, а не анимировать textField, чтобы сделать его видимым.
Спасибо!!!!!
(извините за английские ошибки)
Ответы
Ответ 1
В ViewDidLoad зарегистрируйте уведомления:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide), name:UIKeyboardWillHideNotification, object: nil)
Добавьте ниже методы наблюдателя, которые выполняют автоматическую прокрутку при появлении клавиатуры.
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func keyboardWillShow(notification:NSNotification){
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)
var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = keyboardFrame.size.height
scrollView.contentInset = contentInset
}
func keyboardWillHide(notification:NSNotification){
let contentInset:UIEdgeInsets = UIEdgeInsetsZero
scrollView.contentInset = contentInset
}
РЕДАКТИРОВАТЬ: Эта публикация была обновлена, чтобы отразить лучшие практики в Swift 2.2 для селекторов Objective C.
Ответ 2
Верхний ответ для быстрого 3:
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){
//give room at the bottom of the scroll view, so it doesn't cover up anything the user needs to tap
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = self.theScrollView.contentInset
contentInset.bottom = keyboardFrame.size.height
theScrollView.contentInset = contentInset
}
func keyboardWillHide(notification:NSNotification){
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
theScrollView.contentInset = contentInset
}
Ответ 3
Вот полное решение с использованием осторожного и лаконичного кода. Плюс правильный код в keyboardWillHide
, чтобы сбросить только bottom
на 0.
@IBOutlet weak var scrollView: UIScrollView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
registerNotifications()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
unregisterNotifications()
}
private func registerNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
private func unregisterNotifications() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
scrollView.contentInset.bottom = 0
}
@objc private func keyboardWillShow(notification: NSNotification){
guard let keyboardFrame = notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
scrollView.contentInset.bottom = view.convert(keyboardFrame.cgRectValue, from: nil).size.height
}
@objc private func keyboardWillHide(notification: NSNotification){
scrollView.contentInset.bottom = 0
}
Ответ 4
для Swift 4.0
В ViewDidLoad
// setup keyboard event
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Добавьте ниже методы наблюдателя, которые выполняют автоматическую прокрутку при появлении клавиатуры.
@objc func keyboardWillShow(notification:NSNotification){
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = self.ui_scrollView.contentInset
contentInset.bottom = keyboardFrame.size.height
ui_scrollView.contentInset = contentInset
}
@objc func keyboardWillHide(notification:NSNotification){
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
ui_scrollView.contentInset = contentInset
}
Ответ 5
contentInset
не работает для меня, потому что я хочу, чтобы прокрутка просматривалась выше клавиатуры. Поэтому я использую contentOffset
:
func keyboardWillShow(notification:NSNotification) {
guard let keyboardFrameValue = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else {
return
}
let keyboardFrame = view.convert(keyboardFrameValue.cgRectValue, from: nil)
scrollView.contentOffset = CGPoint(x:0, y:keyboardFrame.size.height)
}
func keyboardWillHide(notification:NSNotification) {
scrollView.contentOffset = .zero
}
Ответ 6
Из ответа Судхира Пальчури, обращенного в Свифт 4.
В 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)
А потом:
// MARK: - Keyboard Delegates
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
@objc func keyboardWillShow(notification:NSNotification){
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = keyboardFrame.size.height
self.scrollView.contentInset = contentInset
}
@objc func keyboardWillHide(notification:NSNotification){
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
self.scrollView.contentInset = contentInset
}
Ответ 7
Читая ссылки, которые вы отправили мне, я нашел способ заставить это работать, спасибо!:
func textFieldDidBeginEditing(textField: UITextField) {
if (textField == //your_field) {
scrollView.setContentOffset(CGPointMake(0, field_extra.center.y-280), animated: true)
callAnimation()
viewDidLayoutSubviews()
}
}
func textFieldDidEndEditing(textField: UITextField) {
if (textField == //your_field){
scrollView .setContentOffset(CGPointMake(0, 0), animated: true)
viewDidLayoutSubviews()
}
}
Ответ 8
Вы можете анимировать свою прокрутку в центре вашего UITextField на экране клавиатуры (т.е. сделать ваше текстовое поле первым ответчиком) с помощью сдвига прокрутки. Вот несколько хороших ресурсов, чтобы вы начали (на этом сайте есть куча):
Как программно перемещать UIScrollView для фокусировки в элементе управления над клавиатурой?
Как сделать автоматическую прокрутку UIScrollView, когда UITextField становится первым ответчиком
Кроме того, если вы просто используете UITableView с вашим контентом в ячейках, когда текстовое поле становится первым ответчиком, UITableViewController автоматически прокручивается до ячейки текстового поля для вас (хотя я не уверен, что это то, что вы хотите сделать).
Ответ 9
Ответ для Swift 3, основанный на том, который был предложен Дэниэлом Джонсом, но более безопасным (благодаря охраннику), более кратким и с последовательными наборами прокрутки:
@objc private func keyboardWillBeShown(notification: NSNotification) {
guard let keyboardFrameValue = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
let keyboardFrame = view.convert(keyboardFrameValue.cgRectValue, from: nil)
scrollView.contentInset.bottom = keyboardFrame.size.height
scrollView.scrollIndicatorInsets = scrollView.contentInset
}
@objc private func keyboardWillBeHidden() {
scrollView.contentInset = .zero
scrollView.scrollIndicatorInsets = scrollView.contentInset
}
Ответ 10
В Swift4 просто добавьте следующее расширение.
extension UIViewController {
func setupViewResizerOnKeyboardShown() {
NotificationCenter.default.addObserver(self,
selector: #selector(self.keyboardWillShowForResizing),
name:
Notification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(self.keyboardWillHideForResizing),
name: Notification.Name.UIKeyboardWillHide,
object: nil)
}
@objc func keyboardWillShowForResizing(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let window = self.view.window?.frame {
// We're not just minusing the kb height from the view height because
// the view could already have been resized for the keyboard before
self.view.frame = CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y,
width: self.view.frame.width,
height: window.origin.y + window.height - keyboardSize.height)
} else {
debugPrint("We're showing the keyboard and either the keyboard size or window is nil: panic widely.")
}
}
@objc func keyboardWillHideForResizing(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let viewHeight = self.view.frame.height
self.view.frame = CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y,
width: self.view.frame.width,
height: viewHeight + keyboardSize.height)
} else {
debugPrint("We're about to hide the keyboard and the keyboard size is nil. Now is the rapture.")
}
}
}
Ответ 11
Swift 5 Настройте ScrollView только тогда, когда TextField скрыт клавиатурой (для нескольких TextFields)
Добавить/удалить наблюдателей:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
Следите за этими значениями, чтобы вы могли вернуться в исходное положение:
var scrollOffset : CGFloat = 0
var distance : CGFloat = 0
Настройте ScrollView contentOffset в зависимости от расположения TextField:
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
var safeArea = self.view.frame
safeArea.size.height += scrollView.contentOffset.y
safeArea.size.height -= keyboardSize.height + (UIScreen.main.bounds.height*0.04) // Adjust buffer to your liking
// determine which UIView was selected and if it is covered by keyboard
let activeField: UIView? = [textFieldA, textViewB, textFieldC].first { $0.isFirstResponder }
if let activeField = activeField {
if safeArea.contains(CGPoint(x: 0, y: activeField.frame.maxY)) {
print("No need to Scroll")
return
} else {
distance = activeField.frame.maxY - safeArea.size.height
scrollOffset = scrollView.contentOffset.y
self.scrollView.setContentOffset(CGPoint(x: 0, y: scrollOffset + distance), animated: true)
}
}
// prevent scrolling while typing
scrollView.isScrollEnabled = false
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if distance == 0 {
return
}
// return to origin scrollOffset
self.scrollView.setContentOffset(CGPoint(x: 0, y: scrollOffset), animated: true)
scrollOffset = 0
distance = 0
scrollView.isScrollEnabled = true
}
Обязательно используйте [UIResponder.keyboardFrameEndUserInfoKey], чтобы получить правильную высоту клавиатуры в первый раз.
Ответ 12
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(_ notification:Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
}
}
func keyboardWillHide(_ notification:Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
}
Ответ 13
В случае, если кто-то ищет код Objective-C для этого решения:
- (void)keyboardWasShown:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = baseScrollView.contentInset;
contentInsets.bottom = kbSize.height;
baseScrollView.contentInset = contentInsets;
}
- (void)keyboardWillBeHidden:(NSNotification *)notification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
baseScrollView.contentInset = contentInsets;
[baseScrollView endEditing:YES];
}