更改UIButton标题导航栏的颜色

时间:2015-05-21 08:26:42

标签: swift ios8 uibutton navigationbar

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

在导航栏中创建按钮:

    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)

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下步骤:

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

  • 在导航项中添加按钮

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

  • 更改颜色