Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-5.0中的UIBarbuttonite颜色设置_Swift_Uibarbuttonitem_Nsattributedstring_Navigationitem - Fatal编程技术网

Swift-5.0中的UIBarbuttonite颜色设置

Swift-5.0中的UIBarbuttonite颜色设置,swift,uibarbuttonitem,nsattributedstring,navigationitem,Swift,Uibarbuttonitem,Nsattributedstring,Navigationitem,我意识到我的uibarbuttoneim(左键和右键)的颜色行为不符合要求 如果我按住右UIBarButton(参见视频),则颜色会从浅黄色变为灰色的深黄色 然而,我想有一个解决方案,保持相同的浅黄色,无论任何按钮选择,按住等。按钮的颜色应始终保持相同的浅黄色 我怎样才能做到这一点 以下是在模拟器中完成的视频: (您可以清楚地看到,点击n-hold会导致颜色变化。即使按住按钮,保持浅黄色的解决方案是什么??) 代码如下: @IBOutlet weak var btnCancel: UIBarB

我意识到我的uibarbuttoneim(左键和右键)的颜色行为不符合要求

如果我按住右UIBarButton(参见视频),则颜色会从浅黄色变为灰色的深黄色

然而,我想有一个解决方案,保持相同的浅黄色,无论任何按钮选择,按住等。按钮的颜色应始终保持相同的浅黄色

我怎样才能做到这一点

以下是在模拟器中完成的视频: (您可以清楚地看到,点击n-hold会导致颜色变化。即使按住按钮,保持浅黄色的解决方案是什么??)

代码如下:

@IBOutlet weak var btnCancel: UIBarButtonItem!
@IBOutlet weak var btnApply: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    btnCancel.title = "Cancel".localized
    btnApply.title = "Apply".localized
    navigationItem.title = "Filter".localized

    let attributes: [NSAttributedString.Key : Any] = [ .font: UIFont(name: "Avenir-Heavy", size: 14)!, .foregroundColor: UIColor.yellow]
    navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
    navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .selected)
    navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .highlighted)
    navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .focused)
}

以下是如何通过将普通按钮包装为barButton项来实现所需效果

    private let normalButton: UIButton = {
        let normalButton = UIButton()
        normalButton.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
        normalButton.setTitle("Apply", for: .normal)
        normalButton.setTitleColor(.yellow, for: .normal)
        normalButton.isUserInteractionEnabled = true
        return normalButton
    }()

    private lazy var applyRightBarButtonItem: UIBarButtonItem = {
        // Wrap your button as UIBarButtonItem
        return UIBarButtonItem(customView: normalButton)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Important for detecting taps
        normalButton.addTarget(self, action: #selector(normalButtonTapped), for: .touchUpInside)
        // Set your right bar button ( You can do the same for the left one)
        self.navigationItem.rightBarButtonItem = applyRightBarButtonItem

    }

    @objc private func normalButtonTapped() {
        // TODO: - Handle tap
        print("Button Tapped")
    }


以下是如何通过将普通按钮包装为barButton项来实现所需效果

    private let normalButton: UIButton = {
        let normalButton = UIButton()
        normalButton.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
        normalButton.setTitle("Apply", for: .normal)
        normalButton.setTitleColor(.yellow, for: .normal)
        normalButton.isUserInteractionEnabled = true
        return normalButton
    }()

    private lazy var applyRightBarButtonItem: UIBarButtonItem = {
        // Wrap your button as UIBarButtonItem
        return UIBarButtonItem(customView: normalButton)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Important for detecting taps
        normalButton.addTarget(self, action: #selector(normalButtonTapped), for: .touchUpInside)
        // Set your right bar button ( You can do the same for the left one)
        self.navigationItem.rightBarButtonItem = applyRightBarButtonItem

    }

    @objc private func normalButtonTapped() {
        // TODO: - Handle tap
        print("Button Tapped")
    }


请尝试以下方法:

// MARK:- Custom BarButton Appearance
private extension YourViewController {

    func setupBarButtonAppearance() {

        let color = UIColor.yellow
        let font = UIFont(name: "Avenir-Heavy", size: 14)!

        let customAppearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [YourViewController.self])

        customAppearance.setTitleTextAttributes([
        NSAttributedString.Key.foregroundColor : color,
        NSAttributedString.Key.font : font], for: .normal)

        customAppearance.setTitleTextAttributes([
        NSAttributedString.Key.foregroundColor : color,
        NSAttributedString.Key.font : font], for: .highlighted)
    }
}

只需在viewDidLoad()中调用此方法即可。

请尝试此方法:

// MARK:- Custom BarButton Appearance
private extension YourViewController {

    func setupBarButtonAppearance() {

        let color = UIColor.yellow
        let font = UIFont(name: "Avenir-Heavy", size: 14)!

        let customAppearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [YourViewController.self])

        customAppearance.setTitleTextAttributes([
        NSAttributedString.Key.foregroundColor : color,
        NSAttributedString.Key.font : font], for: .normal)

        customAppearance.setTitleTextAttributes([
        NSAttributedString.Key.foregroundColor : color,
        NSAttributedString.Key.font : font], for: .highlighted)
    }
}

只需在viewDidLoad()中调用此方法

我认为使用标准UIBarButtonim是不可能的,因为在大多数应用程序中,这种行为是所有UIButtonim的本机和标准行为。您可以使用的一个选项是设置
customView
属性,而不是使用UIBarButtonim的
标题。我不太清楚你为什么要那样想谢谢-我会看看cutomView。“为什么”是由我的设计师驱动的;)我认为使用标准的uibarbuttonite是不可能的,因为在大多数应用程序中,这种行为在所有uibarbuttonite中都是原生的和标准的。您可以使用的一个选项是设置
customView
属性,而不是使用UIBarButtonim的
标题。我不太清楚你为什么要那样想谢谢-我会看看cutomView。“为什么”是由我的设计师驱动的;)