use*_*428 219 uinavigationbar ios swift
我正在使用Picker View来允许用户为整个应用选择颜色主题.我打算改变导航栏,背景和标签栏的颜色(如果可能的话).我一直在研究如何做到这一点,但找不到任何Swift示例.任何人都可以给我一个代码示例,我需要用来更改导航栏颜色和导航栏文本颜色?(Picker View已设置,我只是在寻找更改UI颜色的代码)
谢谢.
tru*_*201 466
导航栏:
navigationController?.navigationBar.barTintColor = UIColor.green
Run Code Online (Sandbox Code Playgroud)
将greenColor替换为您想要的任何UIColor,如果您愿意,也可以使用RGB.
导航栏文字:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
Run Code Online (Sandbox Code Playgroud)
用您喜欢的任何颜色替换orangeColor.
标签栏:
tabBarController?.tabBar.barTintColor = UIColor.brown
Run Code Online (Sandbox Code Playgroud)
标签栏文字:
tabBarController?.tabBar.tintColor = UIColor.yellow
Run Code Online (Sandbox Code Playgroud)
在最后两个中,将brownColor和yellowColor替换为您选择的颜色.
Kee*_*nle 87
以下是一些非常基本的外观自定义,您可以应用于应用范围:
UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
//Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
UITabBar.appearance().backgroundColor = UIColor.yellowColor();
Run Code Online (Sandbox Code Playgroud)
有关UIAppearanceSwift API的更多信息,请参阅此处:https://developer.apple.com/documentation/uikit/uiappearance
Jam*_*mim 50
针对Swift 3,4和4.2进行了更新
// setup navBar.....
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Run Code Online (Sandbox Code Playgroud)
Swift 4.2
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Run Code Online (Sandbox Code Playgroud)
也可以在这里查看:https://github.com/hasnine/iOSUtilitiesSource
Moh*_*mar 49
UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
Run Code Online (Sandbox Code Playgroud)
只需didFinishLaunchingWithOptions在代码中粘贴此行即可.
Dav*_*est 25
在AppDelegate中,这全局改变了NavBar的格式并删除了底线/边框(这是大多数人的问题区域),以便为您提供我认为您和其他人正在寻找的内容:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = Style.SELECTED_COLOR
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = Style.SELECTED_COLOR
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
Run Code Online (Sandbox Code Playgroud)
然后你可以设置一个Constants.swift文件,并包含一个带有颜色和字体等的Style结构.然后你可以将tableView/pickerView添加到任何ViewController并使用"availableThemes"数组来允许用户更改themeColor.
关于这一点的美妙之处在于,您可以在整个应用程序中为每种颜色使用一个引用,它将根据用户选择的"主题"进行更新,而没有一个默认为theme1():
import Foundation
import UIKit
struct Style {
static let availableThemes = ["Theme 1","Theme 2","Theme 3"]
static func loadTheme(){
let defaults = NSUserDefaults.standardUserDefaults()
if let name = defaults.stringForKey("Theme"){
// Select the Theme
if name == availableThemes[0] { theme1() }
if name == availableThemes[1] { theme2() }
if name == availableThemes[2] { theme3() }
}else{
defaults.setObject(availableThemes[0], forKey: "Theme")
theme1()
}
}
// Colors specific to theme - can include multiple colours here for each one
static func theme1(){
static var SELECTED_COLOR = UIColor(red:70/255, green: 38/255, blue: 92/255, alpha: 1) }
static func theme2(){
static var SELECTED_COLOR = UIColor(red:255/255, green: 255/255, blue: 255/255, alpha: 1) }
static func theme3(){
static var SELECTED_COLOR = UIColor(red:90/255, green: 50/255, blue: 120/255, alpha: 1) } ...
Run Code Online (Sandbox Code Playgroud)
Fan*_*ing 18
在storyboard上执行此操作(Interface Builder Inspector)
借助于IBDesignable我们,我们可以为Interface Builder Inspector添加更多选项,UINavigationController并在故事板上进行调整.首先,将以下代码添加到项目中.
@IBDesignable extension UINavigationController {
@IBInspectable var barTintColor: UIColor? {
set {
guard let uiColor = newValue else { return }
navigationBar.barTintColor = uiColor
}
get {
guard let color = navigationBar.barTintColor else { return nil }
return color
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后只需在故事板上设置导航控制器的属性.
此方法还可用于管理故事板中导航栏文本的颜色:
@IBInspectable var barTextColor: UIColor? {
set {
guard let uiColor = newValue else {return}
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: uiColor]
}
get {
guard let textAttributes = navigationBar.titleTextAttributes else { return nil }
return textAttributes[NSAttributedStringKey.foregroundColor] as? UIColor
}
}
Run Code Online (Sandbox Code Playgroud)
Sym*_*mon 18
以下代码适用于 iOS 15
if #available(iOS 15, *) {
// Navigation Bar background color
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.yourColor
// setup title font color
let titleAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.yourColor]
appearance.titleTextAttributes = titleAttribute
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}
Run Code Online (Sandbox Code Playgroud)
Gau*_*gla 15
斯威夫特4:
完美地运行代码以在应用程序级别更改导航栏外观.
// MARK: Navigation Bar Customisation
// To change background colour.
UINavigationBar.appearance().barTintColor = .init(red: 23.0/255, green: 197.0/255, blue: 157.0/255, alpha: 1.0)
// To change colour of tappable items.
UINavigationBar.appearance().tintColor = .white
// To apply textAttributes to title i.e. colour, font etc.
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white,
.font : UIFont.init(name: "AvenirNext-DemiBold", size: 22.0)!]
// To control navigation bar's translucency.
UINavigationBar.appearance().isTranslucent = false
Run Code Online (Sandbox Code Playgroud)
快乐的编码!
ric*_*cks 14
SWIFT 4 - 平滑过渡(最佳解决方案):
如果您从导航控制器返回并且必须在导航控制器上设置不同的颜色,那么您想要使用
override func willMove(toParentViewController parent: UIViewController?) {
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.tintColor = Constants.AppColor
}
Run Code Online (Sandbox Code Playgroud)
而不是将其放在viewWillAppear中,因此转换更清晰.
Mar*_*kus 13
斯威夫特 5(iOS 14)
完整的导航栏定制。
// -----------------------------------------------------------
// NAVIGATION BAR CUSTOMIZATION
// -----------------------------------------------------------
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.isTranslucent = false
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = UIColor.blue
appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
navigationController?.navigationBar.compactAppearance = appearance
} else {
self.navigationController?.navigationBar.barTintColor = UIColor.blue
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
}
// -----------------------------------------------------------
// NAVIGATION BAR SHADOW
// -----------------------------------------------------------
self.navigationController?.navigationBar.layer.masksToBounds = false
self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2)
self.navigationController?.navigationBar.layer.shadowRadius = 15
self.navigationController?.navigationBar.layer.shadowOpacity = 0.7
Run Code Online (Sandbox Code Playgroud)
Doc*_*oca 11
Swift 5,一种带有 UINavigationController 扩展的简单方法。在这个答案的底部是扩展和预览。
第一个视图控制器(主页):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setTintColor(.white)
navigationController?.backgroundColor(.orange)
}
Run Code Online (Sandbox Code Playgroud)
第二个视图控制器(详细信息):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.transparentNavigationBar()
navigationController?.setTintColor(.black)
}
Run Code Online (Sandbox Code Playgroud)
UINavigationController 的扩展:
extension UINavigationController {
func transparentNavigationBar() {
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
}
func setTintColor(_ color: UIColor) {
self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: color]
self.navigationBar.tintColor = color
}
func backgroundColor(_ color: UIColor) {
navigationBar.setBackgroundImage(nil, for: .default)
navigationBar.barTintColor = color
navigationBar.shadowImage = UIImage()
}
}
Run Code Online (Sandbox Code Playgroud)
故事板视图:
预览:
Vin*_*ino 10
在Swift 4中
您可以更改导航栏的颜色.只需使用下面的代码片段即可viewDidLoad()
导航栏颜色
self.navigationController?.navigationBar.barTintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)
导航栏文本颜色
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
Run Code Online (Sandbox Code Playgroud)
对于iOS 11大标题导航栏,您需要使用largeTitleTextAttributes属性
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
Run Code Online (Sandbox Code Playgroud)
外观()功能并不总是适合我.所以我更喜欢创建一个NC对象并更改其属性.
var navBarColor = navigationController!.navigationBar
navBarColor.barTintColor =
UIColor(red: 255/255.0, green: 0/255.0, blue: 0/255.0, alpha: 100.0/100.0)
navBarColor.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.whiteColor()]
Run Code Online (Sandbox Code Playgroud)
此外,如果您想要添加图像而不仅仅是文本,那么也可以
var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
imageView.contentMode = .ScaleAspectFit
var image = UIImage(named: "logo")
imageView.image = image
navigationItem.titleView = imageView
Run Code Online (Sandbox Code Playgroud)
小智 9
在iOS 15中,UIKit将scrollEdgeAppearance的使用扩展到所有导航栏,默认情况下它会产生透明背景。设置scrollEdgeAppearance如下代码。
if #available(iOS 15, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = < your tint color >
navigationController?.navigationBar.standardAppearance = appearance;
navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
}
Run Code Online (Sandbox Code Playgroud)
使用外观API和barTintColor颜色.
UINavigationBar.appearance().barTintColor = UIColor.greenColor()
Run Code Online (Sandbox Code Playgroud)
小智 7
这些解决方案都不适合我,所以我分享了一个有效的解决方案。
\n斯威夫特 5、Xcode 13.4.1
\n将以下内容放入 viewDidLoad() 中:
\nlet appearance = UINavigationBarAppearance()\n appearance.configureWithOpaqueBackground()\n appearance.backgroundColor = UIColor.systemBlue\n appearance.titleTextAttributes = [.foregroundColor: UIColor.white]\n navigationItem.standardAppearance = appearance\n navigationItem.scrollEdgeAppearance = appearance\n navigationItem.compactAppearance = appearance\nRun Code Online (Sandbox Code Playgroud)\n这是结果
\n\n请记住将检查器中的所有设置设置为默认值。如果您需要更多调整,请通过开发人员文档搜索“自定义您的应用程序\xe2\x80\x99s 导航栏”
\n希望有帮助。
\n此版本还移除了导航栏下的 1px 阴影线:
Swift 5:把它放在你的AppDelegate didFinishLaunchingWithOptions
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
Run Code Online (Sandbox Code Playgroud)