首页 > 解决方案 > 如何在键盘外观上向上移动整个视图,包括选择器视图

问题描述

我有这种情况,我有一个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 的文本字段的名称。我任何人都可以帮助我,我将不胜感激。

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

向上视图的示例图像

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

标签: swift

解决方案


首先,您需要在键盘变得可见时收到通知

我们通过在应用程序的任何地方添加一个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)

我会让你自己试试那个;)

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


推荐阅读