如何将整个视图(包括选择器视图)在键盘上向上移动



我有这种情况,我有一个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的文本字段的名称。如果有人能帮助我,我会很感激的。

我试着将整个视图向上移动。它看起来像下面的照片。

上升视图样图

在这张照片中,选择器视图仍然在底部

首先,你需要收到一个通知,当键盘变得可见

我们通过在应用的任何地方添加一个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后跟通知名称。看到

接下来,我们可以监听这个通知何时被触发

//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)

我让你自己试一试;)

有关更多信息,请查看此处管理键盘的文档

相关内容

  • 没有找到相关文章

最新更新