Bra*_*ans 174 uinavigationbar uibarbuttonitem ios backbarbuttonitem swift
我正在尝试将"设置"按钮的颜色更改为白色,但无法将其更改.
我试过这两个:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)
但没有变化,它仍然看起来像这样:

如何将该按钮设为白色?
Etg*_*gar 252
您可以通过单击电路板上的空白区域并在右侧工具栏中选择"显示文件检查器"来更改故事板中的全局色调颜色,您将在工具栏的底部看到"全局色调"选项.
Cha*_*van 189
此代码更改箭头颜色
self.navigationController.navigationBar.tintColor = UIColor.whiteColor();
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,请使用以下代码:
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)
Swift 3笔记
UIColor.whiteColor() 和类似的已经简化为 UIColor.white
此外,许多以前隐式的选项已更改为显式,因此您可能需要:
self.navigationController?.navigationBar =
Run Code Online (Sandbox Code Playgroud)
Tie*_*Van 50
你应该用这个:
navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
Run Code Online (Sandbox Code Playgroud)
Dee*_*iya 26
斯威夫特3
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)
Moh*_*din 19
你可以像这样使用.放在里面AppDelegate.swift.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
return true
}
Run Code Online (Sandbox Code Playgroud)
AiO*_*OsN 16
更改完整的应用主题
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().tintColor = .white
return true
}
Run Code Online (Sandbox Code Playgroud)
更改特定控制器
let navController = UINavigationController.init(rootViewController: yourViewController)
navController.navigationBar.tintColor = .red
present(navController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
Vin*_*ent 14
在Swift3中,要将"后退"按钮设置为red.
self.navigationController?.navigationBar.tintColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)
小智 13
在Swift 4中,您可以使用以下方法处理此问题:
let navStyles = UINavigationBar.appearance()
// This will set the color of the text for the back buttons.
navStyles.tintColor = .white
// This will set the background color for navBar
navStyles.barTintColor = .black
Run Code Online (Sandbox Code Playgroud)
self.navigationController?.navigationBar.tintColor = UIColor.black // to change the all text color in navigation bar or navigation
self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color
Run Code Online (Sandbox Code Playgroud)
小智 9
如果您尝试了多次但无法成功,您可以尝试:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red
Run Code Online (Sandbox Code Playgroud)
其实我尝试了很多次,只发现这个方法可行。
Swift 5 更新
如果您需要Back全局设置按钮颜色,您可以简单地使用:
UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color
Run Code Online (Sandbox Code Playgroud)
那么您不需要在每个视图控制器上设置后退按钮背景颜色。如果您使用此控制器,则无法在另一个视图控制器上设置后退按钮颜色
但
如果您需要在视图控制器上设置后退按钮颜色或在另一个视图控制器上更改,请不要使用上述方法。你可以使用:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font:FontFamily.BatonTurbo.medium.font(size: 20),
.foregroundColor: Asset.pureWhite.color] // Naviagtion Title attributes
appearance.backgroundColor = .red // Navigation bar background color
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance
navigationController?.navigationBar.tintColor = .green // Back button color
Run Code Online (Sandbox Code Playgroud)
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)
这个片段带来了魔力.而不是redColor,将其更改为您的愿望.
小智 6
斯威夫特3
对于Swift 3,最受欢迎的答案是不正确的.
更改颜色的正确代码是:
self.navigationController?.navigationBar.tintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)
如果要更改颜色,请将上面的UIColor.white更改为所需的颜色
小智 6
在AppDelegate课堂上使用此代码didFinishLaunchingWithOptions.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .white
}
Run Code Online (Sandbox Code Playgroud)
设置的所有答案都UINavigationBar.appearance().tintColor与Apple中的文档冲突UIAppearance.h。
针对iOS7的注意事项:在iOS7上,该
tintColor属性已移至UIView,现在具有中所述的特殊继承行为UIView.h。此继承的行为可能与外观代理发生冲突,因此tintColor现在禁止外观代理使用。
在Xcode中,您需要在要与外观代理一起使用的每个属性上单击命令,以检查头文件并确保该属性使用注释UI_APPEARANCE_SELECTOR。
因此,通过外观代理将整个应用程序中的导航栏着色为紫色,将标题和按钮着色为白色的正确方法是:
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().tintColor = .white
Run Code Online (Sandbox Code Playgroud)
请注意,这UIBarButtonItem不是的子类,UIView而是的子类NSObject。因此,它的tintColor属性不是从继承tintColor的UIView。
不幸的是,UIBarButtonItem.tintColor没有注释UI_APPEARANCE_SELECTOR-但这在我看来是一个文档错误。Apple Engineering在此雷达中的响应表明它受到支持。
| 归档时间: |
|
| 查看次数: |
140595 次 |
| 最近记录: |