将文本框输入视图从pickerview更改为swift 3.0中的键盘

时间:2017-02-16 12:31:17

标签: swift swift3

在文本框上单击pickerview应该raise.in选择器视图我添加了这样的工具

picker.delegate = self
picker.dataSource = self
picker.showsSelectionIndicator = true
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor.black
    //UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()

let addbutton = UIBarButtonItem(title: "+Add", style: UIBarButtonItemStyle.plain, target: self, action: #selector(GBEDasboardViewController.addproducts))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(GBEDasboardViewController.addproducts))

toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true

productname.inputView = picker
productname.inputAccessoryView = toolBar

点击addbutton pickerview应该关闭,键盘应该提升

     func addproducts(){
      self.view.endEditing(true)
    }

pickerview正在关闭,但如何提高键盘。

1 个答案:

答案 0 :(得分:0)

您应该用以下代码替换您的代码。

let addbutton = UIBarButtonItem(title: "+Add", style: UIBarButtonItemStyle.plain, target: self, action: #selector(GBEDasboardViewController.addProducts))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(GBEDasboardViewController.endEditing))

func addProducts(){
      self.view.endEditing(true)
      productname.inputView = nil
      productname.becomeFirstResponder()
 }

 func endEditing() {
    self.view.endEditing(true)

 }