ios - 当导航栏中的 UISearchBar 是 firstResponder 时,UIButton 不调用操作
问题描述
我希望UIButton
在UIViewController
用户编辑UISearchBar
属于UINavigationBar
. UIButton
正在响应触摸(它显示突出显示的状态),但它没有调用它的动作。其他UIControl
类(如UISwitch
.
为了使它更有趣:将'sUISearchBar
放在UIViewController
'sUIView
中时,没有问题。
我应该怎么做才能在UISearchBar
激活时触发按钮操作?
例VC:
import UIKit
class ViewController: UIViewController {
private let button: UIButton = {
let button = UIButton(type: UIButtonType.system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Test", for: .normal)
button.setTitleColor(.green, for: .normal)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return button
}()
private let textField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "place"
textField.borderStyle = .bezel
textField.addTarget(self, action: #selector(textFieldTriggered), for: .allEditingEvents)
return textField
}()
private let uiSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.translatesAutoresizingMaskIntoConstraints = false
uiSwitch.addTarget(self, action: #selector(switchToggled), for: UIControlEvents.valueChanged)
return uiSwitch
}()
private let formSearchBar: UISearchBar = {
let searchBar = UISearchBar()
searchBar.translatesAutoresizingMaskIntoConstraints = false
return searchBar
}()
private let navigationSearchBar: UISearchBar = {
return UISearchBar()
}()
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(button)
view.addSubview(textField)
view.addSubview(formSearchBar)
view.addSubview(uiSwitch)
NSLayoutConstraint.activate([
view.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: button.leadingAnchor, constant: -16),
view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: button.topAnchor, constant: -16),
button.bottomAnchor.constraint(equalTo: textField.topAnchor, constant: -16),
view.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: textField.leadingAnchor, constant: -16),
textField.widthAnchor.constraint(equalToConstant: 100),
textField.bottomAnchor.constraint(equalTo: formSearchBar.topAnchor, constant: -16),
view.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: formSearchBar.leadingAnchor, constant: -16),
formSearchBar.widthAnchor.constraint(equalToConstant: 100),
formSearchBar.bottomAnchor.constraint(equalTo: uiSwitch.topAnchor, constant: -16),
view.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: uiSwitch.leadingAnchor, constant: -16),
])
navigationItem.titleView = navigationSearchBar
_ = navigationSearchBar.becomeFirstResponder()
}
@objc
private func buttonTapped() {
let color = button.titleColor(for: .normal)
button.setTitleColor(color == .green ? .red : .green, for: .normal)
}
@objc
private func textFieldTriggered() {
textField.placeholder = textField.placeholder == "place" ? "holder" : "place"
}
@objc
private func switchToggled() {
UIView.animate(withDuration: 0.3) {
self.uiSwitch.isOn = !self.uiSwitch.isOn
}
}
}
解决方案
推荐阅读
- c++ - 在 C++ 目标中设置源文件名
- gerrit - 如何使用 API 丢弃关于 gerrit 的草稿评论?
- python - dtreeviz 替换绘图,回归太多点
- android - 使用 Coordinator 布局时,SnackBar 显示在某个高度之上
- python - 使用 Google Adword API 时收到此错误 OperatorError.OPERATOR_NOT_SUPPORTED @ operations[0]
- algorithm - 找到这样一种方式,即在途中跳跃的最大距离可能是最小的
- css - 隐藏在 Accordion bootstrap 后面的 react-datetime
- javascript - 偶尔在保存导入时添加MissingImports 再次反应
- javascript - 如何使链接包含文本并显示在文本框中
- javascript - NodeJS MonogoDB Postman Post 请求引发错误,可能是 objectID 错误