【问题标题】:Changing colour of navigation bar改变导航栏的颜色
【发布时间】:2018-08-21 08:02:22
【问题描述】:

我正在尝试更改导航栏的背景颜色,但遇到了问题:

let colourRGBs: [[CGFloat]] = [[247, 247, 247], [38, 123, 238], [93, 204, 3]]
let colourTitles: [String] = ["Default", "Blue", "Green"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return colourTitles.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PickNavBarColourCell", for: indexPath) as! PickNavBarColourCell
    let RGB = colourRGBs[indexPath.row]
    cell.backgroundColor = UIColor(red: RGB[0]/255.0, green: RGB[1]/255.0, blue: RGB[2]/255.0, alpha: 1.0)
    cell.configureCell(text: colourTitles[indexPath.row])
    if indexPath.row == passOnNavBarColour.colour {
        cell.accessoryType = UITableViewCellAccessoryType.checkmark
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryType.none
    }

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    UserDefaults.standard.set(indexPath.row, forKey: "NavBarColour")
    passOnNavBarColour.colour = indexPath.row
    tableView.reloadData()
    let RGB = colourRGBs[indexPath.row]
    self.navigationController?.navigationBar.backgroundColor = UIColor(red: RGB[0]/255.0, green: RGB[1]/255.0, blue: RGB[2]/255.0, alpha: 1.0)
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44
}

例如,当我为蓝色执行此操作时,结果如下:

它比应有的要暗得多。

我尝试了以下代码:

self.navigationController?.navigationBar.isTranslucent = false

我在设置单元格背景颜色之前运行的

self.navigationController?.navigationBar.backgroundColor = UIColor(red: RGB[0]/255.0, green: RGB[1]/255.0, blue: RGB[2]/255.0, alpha: 1.0)

但是这不会导致任何颜色变化。

我该如何解决这个问题?

【问题讨论】:

  • 您是否将以下代码放入 viewDidLoad 方法?
  • 否 - 该代码属于整个视图控制器

标签: ios swift xcode uinavigationcontroller uinavigationbar


【解决方案1】:

导航栏:

navigationController?.navigationBar.barTintColor = UIColor.green

将 greenColor 替换为您想要的任何 UIColor,如果您愿意,也可以使用 RGB。

导航栏文字:

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange] 

将 orangeColor 替换为您喜欢的任何颜色。

标签栏:

tabBarController?.tabBar.barTintColor = UIColor.brown 

标签栏文本:

tabBarController?.tabBar.tintColor = UIColor.yellow 

在最后两个中,将 brownColor 和 yellowColor 替换为您选择的颜色。

【讨论】:

    【解决方案2】:

    使用navigationBar.barTintColor

    来自 Apple 的文档:https://developer.apple.com/documentation/uikit/uinavigationbar/#1654191

    您可以使用 barTintColor 属性为导航栏背景指定自定义色调颜色。设置此属性会覆盖从条形样式推断的默认颜色。与所有 UIView 子类一样,您可以使用 tintColor 属性控制导航栏中交互元素的颜色,包括按钮图像和标题。

    无法更改navigationBar.backgroundColor,因为它被一些视图隐藏。 您可以通过断点查看视图层次结构。

    【讨论】:

    • 谢谢,这很有帮助! :)
    【解决方案3】:

    你可以在didFinishLaunchingWithOptionsAppDelegate中添加这个简单的行

    UINavigationBar.appearance().barTintColor = UIColor.blue
    

    【讨论】:

      猜你喜欢
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多