【问题标题】:Change color of the UIButton title navigation bar更改 UIButton 标题导航栏的颜色
【发布时间】:2015-05-21 08:26:41
【问题描述】:

当我创建导航栏时,我可以在左侧项目、右侧项目和标题视图中放置三个按钮,但只有左右项目有动画,并且我不能更改标题按钮的颜色。

导航栏中按钮的创建:

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "VOYAGE", style: UIBarButtonItemStyle.Plain, target: self, action: "goToPage1:")

    var buttonCenter = UIButton()
    buttonCenter.setTitle("PHOTOS", forState: .Normal)
    buttonCenter.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    buttonCenter.addTarget(self, action: "goToPage2:", forControlEvents: .TouchUpInside)
    self.navigationItem.titleView = buttonCenter

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "SIGHTINGS", style: UIBarButtonItemStyle.Plain, target: self, action: "goToPage3:")

触摸按钮时颜色的变化:

函数 goToPage1 :

    self.navigationItem.leftBarButtonItem!.tintColor = UIColor(red: 174/255, green: 211/255, blue: 43/255, alpha: 1)
    self.navigationItem.titleView!.tintColor = UIColor.whiteColor()
    self.navigationItem.rightBarButtonItem!.tintColor = UIColor.whiteColor()

函数 goToPage2 :

    self.navigationItem.leftBarButtonItem!.tintColor = UIColor.whiteColor()
    self.navigationItem.titleView!.tintColor = UIColor(red: 174/255, green: 211/255, blue: 43/255, alpha: 1)
    self.navigationItem.rightBarButtonItem!.tintColor = UIColor.whiteColor()

函数 goToPage3 :

    self.navigationItem.leftBarButtonItem!.tintColor = UIColor.whiteColor()
    self.navigationItem.titleView!.tintColor = UIColor.whiteColor()
    self.navigationItem.rightBarButtonItem!.tintColor = UIColor(red: 174/255, green: 211/255, blue: 43/255, alpha: 1)

你对按钮的选择动画有什么想法吗?

感谢您的帮助。

【问题讨论】:

  • 我可能错了,但我认为您需要更改按钮中心的颜色而不是 titleView 了
  • 我试过但没用
  • 你用了吗 buttonCenter.backgroundColor = UIColor.whiteColor()
  • 我已经创建了“var buttonCenter: UIButton?”我启动了 viewDidLoad 中的按钮,但颜色没有改变。
  • 也许您可以尝试设置按钮颜色并将其重新分配给 navigationItem.titleView 每次您希望更改颜色时。例如:buttonCenter.setTitleColor(UIColor.whiteColor(), forState: .Normal) self.navigationItem.titleView = buttonCenter

标签: swift ios8 uibutton navigationbar


【解决方案1】:

你可以试试这个步骤:

  • 在 main.storyboard 中,对于每个视图,您可以在模拟指标部分将顶部栏的值更改为半透明导航栏

  • 添加导航项创建您的按钮

  • 将 main.storyboard 中的按钮链接到您的文件并使用 self.yourButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) 更改颜色

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2023-03-19
    • 1970-01-01
    • 2020-10-27
    • 2023-03-09
    • 2018-02-06
    相关资源
    最近更新 更多