首页 > 解决方案 > 当导航栏中的 UISearchBar 是 firstResponder 时,UIButton 不调用操作

问题描述

我希望UIButtonUIViewController用户编辑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
        }
    }
}

标签: iosuikit

解决方案


推荐阅读