Ios 更改导航栏的颜色
我试图更改导航栏的背景颜色,但遇到问题:Ios 更改导航栏的颜色,ios,swift,xcode,uinavigationcontroller,uinavigationbar,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 t
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
}
例如,当我对blue执行此操作时,结果如下:
它比应该的要暗得多
我已使用以下代码进行了尝试:
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)
然而,这不会导致任何颜色变化
如何解决此问题?导航栏:
navigationController?.navigationBar.barTintColor = UIColor.green
用你想要的任何颜色替换绿色,如果你喜欢,你也可以使用RGB
导航栏文本:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
tabBarController?.tabBar.tintColor = UIColor.yellow
用你喜欢的颜色替换橙色
选项卡栏:
tabBarController?.tabBar.barTintColor = UIColor.brown
选项卡栏文本:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
tabBarController?.tabBar.tintColor = UIColor.yellow
在最后两种颜色中,将brownColor和yellowColor替换为您选择的颜色。导航栏:
navigationController?.navigationBar.barTintColor = UIColor.green
用你想要的任何颜色替换绿色,如果你喜欢,你也可以使用RGB
导航栏文本:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
tabBarController?.tabBar.tintColor = UIColor.yellow
用你喜欢的颜色替换橙色
选项卡栏:
tabBarController?.tabBar.barTintColor = UIColor.brown
选项卡栏文本:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
tabBarController?.tabBar.tintColor = UIColor.yellow
在最后两个选项中,将brownColor和yellowColor替换为您选择的颜色。您可以在
AppDelegate
中的didFinishLaunchingWithOptions
中添加此简单行
UINavigationBar.appearance().barTintColor = UIColor.blue
您可以在
AppDelegate
UINavigationBar.appearance().barTintColor = UIColor.blue
使用
navigationBar.barTintColor
来自苹果公司的文件:
可以使用barTintColor属性为导航栏背景指定自定义着色颜色。设置此特性将替代从条形样式推断的默认颜色。与所有UIView子类一样,您可以使用tintColor属性控制导航栏中交互元素的颜色,包括按钮图像和标题
无法更改导航栏。背景颜色,因为它被一些视图隐藏。
可以通过断点查看视图层次
使用
导航栏.barTintColor
来自苹果公司的文件:
可以使用barTintColor属性为导航栏背景指定自定义着色颜色。设置此特性将替代从条形样式推断的默认颜色。与所有UIView子类一样,您可以使用tintColor属性控制导航栏中交互元素的颜色,包括按钮图像和标题
无法更改导航栏。背景颜色,因为它被一些视图隐藏。
可以通过断点查看视图层次
您是否将以下代码放在viewDidLoad方法中?否-该代码属于整个视图控制器ID您将以下代码放在viewDidLoad方法中?否-该代码属于整个视图控制器谢谢这非常有帮助!:)谢谢,这很有帮助!:)