ios - 改变导航栏的颜色

标签 ios swift xcode uinavigationcontroller uinavigationbar

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

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
}

例如,当我对蓝色执行此操作时,结果如下: enter image description here

它比应该的要暗得多。

我尝试了以下代码:

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)

然而,这不会导致任何颜色变化。

我该如何解决?

最佳答案

使用navigationBar.barTintColor

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

You can specify a custom tint color for a navigation bar background using the barTintColor property. Setting this property overrides the default color inferred from the bar style. As with all UIView subclasses, you can control the color of the interactive elements within navigation bars, including button images and titles, using the tintColor property.

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

enter image description here

关于ios - 改变导航栏的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51944214/

相关文章:

iOS UIImage 转换为固定内存大小

ios - UITableView 重新加载部分顺序很重要吗?

ios - URL 请求中发送页面编号问题。获得成功但结果空洞

ios - [可能重复]:体系结构i386的 undefined symbol :

ios - 如何为 AVAudioEngine 设置完成处理程序

ios - 通过隐藏和显示 View 在 iMessage 应用程序中使用不同的 View

iphone - 将字符串转换为 NSDate 问题 : [__NSDate Length] unrecognized selected

ios - 当表格未处于编辑模式时,我可以使用 UITableViewCellEditingStyleInsert 吗?

swift - 不安全的可变寻址器崩溃

ios - 当以编程方式设置 Root View Controller 时,导航和选项卡栏丢失