Mel*_*or 2 uinavigationbar uistatusbar swift
我想将状态栏颜色设置为与导航栏相同的颜色。当我尝试将导航栏和状态栏设置为相同的颜色时,导航栏总是以比状态栏更浅的颜色显示。
这就是我要的:
我的结果:
AppDelegate中的代码:
状态栏:
UIApplication.sharedApplication().statusBarStyle = .Default
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
if statusBar.respondsToSelector(Selector("setBackgroundColor:"))
{
statusBar.backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
statusBar.tintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
}
Run Code Online (Sandbox Code Playgroud)
导航栏:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UIApplication.sharedApplication().statusBarHidden = false
UIApplication.sharedApplication().statusBarStyle = .Default
Run Code Online (Sandbox Code Playgroud)
谁能给我关于如何解决这个问题的任何建议。
提前致谢!
我毫无疑问地得到了预期的结果:
这是我使用的代码(Swift 3):
let app = UINavigationBar.appearance()
// nav bar color => your color
app.barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
app.isTranslucent = false
// status bar text => white
app.barStyle = .black
// nav bar elements color => white
app.tintColor = .white
app.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
Run Code Online (Sandbox Code Playgroud)
至于您在做什么,很难知道从哪里开始。首先,此行是完全非法的(并且很可能会导致您的应用被App Store禁止):
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
Run Code Online (Sandbox Code Playgroud)
这些天,iOS中状态栏的颜色很清晰。无需设置其颜色,也不应设置颜色。如果行为正确,导航栏将在状态栏后面向上延伸,因此它们将始终具有相同的颜色,因为您看到的始终是相同的界面对象,即导航栏。
至于设置导航栏的颜色,请不要忘记默认情况下它是半透明的。不使其不透明就无法准确设置其颜色。另外,您不应设置其backgroundColor。您应该设置其barTintColor背景颜色,或者为其提供背景图像(这是对其颜色进行最佳控制的方法)。
简而言之,您所做的一切都是错误的,因此很难对它不起作用的事实表示同情。
| 归档时间: |
|
| 查看次数: |
5265 次 |
| 最近记录: |