在 Xcode 中更改导航栏颜色
Changing Navigation Bar Color in Xcode
我正在构建一个 Swift 应用程序,我正在尝试更改导航栏的颜色。
我正在尝试直接从 Xcode 执行此操作,而无需 main.storyboard.
上的代码
我已经尝试更改 Xcode 属性检查器中的几个设置,但没有任何效果。我还搜索了 Whosebug,但我找到的所有答案都是以编程方式更改它。
有谁知道如何直接从属性检查器更改它的颜色?
谢谢。
试一试:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(red: 21/255, green: 46/255, blue: 82/255, alpha: 1)
appearance.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont(name: "Arial", size: 32)!]
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
navigationController?.navigationBar.compactAppearance = appearance.copy()
}
您可以使用
navigationBar.barTintColor = UIColor.whiteColor()
或者你像这样通过 Storyboard 做这个。
在故事板中,更改背景颜色。它会起作用。
您只需点击导航栏,在属性检查器中,有一个背景选项,您可以提供任何您想要的颜色。就我而言,我选择了自定义的天蓝色。
导航栏:
navigationController?.navigationBar.barTintColor = UIColor.green
用你想要的任何 UIColor 替换 greenColor
导航栏文本:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
将 orangeColor 替换为您喜欢的任何颜色。
标签栏:
tabBarController?.tabBar.barTintColor = UIColor.brown
标签栏文本:
tabBarController?.tabBar.tintColor = UIColor.yellow
最后两个,将 brownColor 和 yellowColor 替换为您选择的颜色。
swift导航栏标题颜色:
将它放在 AppDelegate 的 didFinishLaunchingWithOptions 方法中。
let attrs = [
NSAttributedString.Key.foregroundColor: UIColor.white
]
UINavigationBar.appearance().titleTextAttributes = attrs
我正在构建一个 Swift 应用程序,我正在尝试更改导航栏的颜色。 我正在尝试直接从 Xcode 执行此操作,而无需 main.storyboard.
上的代码我已经尝试更改 Xcode 属性检查器中的几个设置,但没有任何效果。我还搜索了 Whosebug,但我找到的所有答案都是以编程方式更改它。
有谁知道如何直接从属性检查器更改它的颜色?
谢谢。
试一试:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(red: 21/255, green: 46/255, blue: 82/255, alpha: 1)
appearance.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont(name: "Arial", size: 32)!]
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
navigationController?.navigationBar.compactAppearance = appearance.copy()
}
您可以使用
navigationBar.barTintColor = UIColor.whiteColor()
或者你像这样通过 Storyboard 做这个。
在故事板中,更改背景颜色。它会起作用。
您只需点击导航栏,在属性检查器中,有一个背景选项,您可以提供任何您想要的颜色。就我而言,我选择了自定义的天蓝色。
导航栏:
navigationController?.navigationBar.barTintColor = UIColor.green
用你想要的任何 UIColor 替换 greenColor
导航栏文本:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
将 orangeColor 替换为您喜欢的任何颜色。
标签栏:
tabBarController?.tabBar.barTintColor = UIColor.brown
标签栏文本:
tabBarController?.tabBar.tintColor = UIColor.yellow
最后两个,将 brownColor 和 yellowColor 替换为您选择的颜色。
swift导航栏标题颜色:
将它放在 AppDelegate 的 didFinishLaunchingWithOptions 方法中。
let attrs = [
NSAttributedString.Key.foregroundColor: UIColor.white
]
UINavigationBar.appearance().titleTextAttributes = attrs