在iOS swift中单击文本字段时,是否可以显示操作表并隐藏键盘?
当我点击文本字段时,我试图从操作表中获取文本字段的值。我不想在单击文本字段时显示键盘。在iOS swift中单击文本字段时,是否可以显示操作表并隐藏键盘?,ios,xcode,swift3,uitextfield,Ios,Xcode,Swift3,Uitextfield,当我点击文本字段时,我试图从操作表中获取文本字段的值。我不想在单击文本字段时显示键盘。 实际上我需要uitextfield中的button click属性。可以这样做吗?试试这个。它应该适合您,首先为textfield设置委托 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { textField.resignFirstResponder() let ac
实际上我需要uitextfield中的button click属性。可以这样做吗?试试这个。它应该适合您,首先为textfield设置委托
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
let actionSheet = UIActionSheet(title: "Select", delegate: self, cancelButtonTitle: "Yes", destructiveButtonTitle: "", otherButtonTitles: "A", "B")
actionSheet.actionSheetStyle = .default
actionSheet.show(in: self.view)
return false
}
文本字段应开始编辑:
当用户执行以下操作时,文本字段调用此方法:
通常会启动文本字段文本的编辑
使用UITextFieldDelegate: 1-将代理添加到文本字段:
yourTextField.deleagete = self
2-像这样使用委托
func textFieldDidBeginEditing(textField: UITextField) {
textField.resignFirstResponder()
//call your function here
}
斯威夫特4
private var positionArray = ["All", "QB", "RB", "WR", "TE", "K", "DE", "OL", "LB", "EDGE", "DT", "CB", "S"]
//1将代理添加到文本字段中
tf.delegate = self
//2扩展UITextfield代理
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// to avoid keyboard
textField.resignFirstResponder()
// add action sheet
let alert = UIAlertController(title: LocalString("draft_title"), message: LocalString("draft_select_position"), preferredStyle: .actionSheet)
for i in positionArray {
alert.addAction(UIAlertAction(title: i, style: .default , handler:{ (UIAlertAction)in
textfield.text = i
self.view2021Draft?.positionSelected(isSelected: true, positionValue: textfield.text ?? "")
}))
}
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
self.view2021Draft?.positionSelected(isSelected: false, positionValue: textfield.text ?? "")
}))
//uncomment for iPad Support
alert.popoverPresentationController?.sourceView = self.view
self.present(alert, animated: true, completion: {
print("completion block")
})
return false
}
为什么不让按钮看起来像一个文本字段?触摸按钮并显示操作表。@JefferyThomas我需要天空浮动文本字段itself@JefferyThomas我想在文本字段上方放置一个按钮有任何简单的方法可以使用相同的约束来放置两者是的,您可以将约束放置在一个上(不管是哪个),第二个约束被约束到第一个(从上到上、从左到左、从下到下、从右到右)。这样,当主约束的约束更改时,另一个约束将自动拟合一次。