如何在键盘外观上向上移动包括pickerview在内的整个视图
Posted
技术标签:
【中文标题】如何在键盘外观上向上移动包括pickerview在内的整个视图【英文标题】:How Can I shift the entire view including the picker view Upward on keyboard appearence 【发布时间】:2021-09-03 08:24:01 【问题描述】:我有这种情况,我有一个pickerView 作为工具栏,在pickerView 上方有搜索栏。现在,当我点击搜索栏时,键盘会出现,我希望包含工具栏的 pickerView 向上移动。
这是pickerView的一些代码-
var picker = UIPickerView()
picker = UIPickerView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
picker.delegate = self
picker.dataSource = self
tfCountry.inputView = picker
tfCountry.inputView?.backgroundColor = UIColor.black.withAlphaComponent(0.82)
searchBar.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 20)
searchBar.placeholder = "Search Country"
let leftNavBarButton = UIBarButtonItem(customView:searchBar)
searchBar.delegate = self
searchBar.searchTextField.textColor = .white
searchBar.sizeToFit()
let toolBar = UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: screenWidth, height: 44.0))
toolBar.barTintColor = UIColor.black.withAlphaComponent(0.82)
toolBar.sizeToFit()
toolBar.setItems([leftNavBarButton], animated: false)
tfCountry.inputAccessoryView = toolBar
tfCountry 是触发 pickerView 的文本字段的名称。任何人都可以帮助我,我将不胜感激。
我尝试将整个视图向上移动。它看起来像下面的照片中的线条。
sample image of up lifiting view
在这张照片中,选择器视图仍然保留在底部
【问题讨论】:
【参考方案1】:首先,您需要在键盘变得可见时收到通知
我们通过在应用程序的任意位置添加NSNotification 来实现这一点,它会监听特定的变化(在本例中是键盘显示/隐藏事件)。然后我们可以订阅它以更改视图的位置。 (往下看)
func registerForKeyboardNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeShown(notification:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
注意:建议您在使用完这些通知后也将其删除,以避免内存泄漏/性能问题。这是类似的语法,但我们使用removeObserver
后跟通知名称。 See here
接下来,我们可以监听这个通知何时被触发
//Ideally this would be in your view
//You'll want to use a scrollView so we can easily scroll to the content when it is covered by the keyboard
//This scrollView will have to be added to your parent view or whatever structure you are using
let scrollView = UIScrollView()
//The target for the scroll
let textField = UITextField()
func keyboardWasShown(notification: NSNotification)
//Should be disabled by default
self.scrollView.isScrollEnabled = true
//An object containing information about the keyboard event
if let info = notification.userInfo
//We'll need the keyboard size in order to know how much to bump the view by
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
guard keyboardSize != nil else return
//New view height now determined on the height of the keyboard
let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0)
//Adding these positions to the scrollView, you will have to keep in mind where your UIPickerView is and where you want it to be when the keyboard is visible, but this example should be similar to what you need to achieve
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
//Get frame object
let rect: CGRect = self.view.frame
//make height size of frame - keyboard
rect.size.height -= keyboardSize.height
//Check if textField exists
if let activeField = self.textField
//If it does and the frame contains the textField, scroll to it
if (!rect.contains(activeField.frame.origin))
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
最后,当键盘消失时,执行相反的操作以将视图恢复正常
和之前一样,您需要创建一个函数来接收键盘被隐藏事件:
func keyboardWillBeHidden(notification: NSNotification)
我会让你自己试试那个;)
有关更多信息,请查看有关管理键盘的文档here
【讨论】:
以上是关于如何在键盘外观上向上移动包括pickerview在内的整个视图的主要内容,如果未能解决你的问题,请参考以下文章