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