如何在Swift中设置状态栏风格3
我正在使用Xcode 8.0 beta 4。
在以前的版本中,UIViewController有设置状态栏风格的方法
public func preferredStatusBarStyle() -> UIStatusBarStyle
但是,我发现它在Swift 3中变成了“只可变varaiable”。
public var preferredStatusBarStyle: UIStatusBarStyle { get }
如何提供在我的UIViewController中使用的样式?
ios 10和swift 3
-
在info.plist中更改该行查看基于控制器的状态栏外观 ,并将其设置为NO
-
更改didFinishLaunchingWithOptions中的appDelegate.swift
UIApplication.shared.statusBarStyle = .lightContent
您可以尝试覆盖返回的值,而不是设置它。 该方法被声明为{get},所以只需要提供一个getter:
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
如果您有条件地设置,则需要调用setNeedsStatusBarAppearanceUpdate()
以便在准备就绪时生成更改
Swift 3,iOS 10
对我而言,这种方法不起作用:
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
当我使用每个视图控制器,但这工作:
-
在文件info.list中,添加行:
View controller-based status bar appearance
并设置为NO
-
接下来在appdelegate:
UIApplication.shared.statusBarStyle = .lightContent
您需要在Info.plist文件中添加下面的键:
View controller-based status bar appearance
,布尔值设置为NO
在你的appdelegate类中,在didFinishLaunchingWithOptions
方法返回之前。
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) { statusBar.backgroundColor = UIColor.red } UIApplication.shared.statusBarStyle = .lightContent
根据需要更改backgroundColor
和statusBarStyle
。
如果你想在视图出现后的任何时候改变状态栏的样式,你可以使用这个:
-
在文件info.list中添加行: 查看基于控制器的状态栏外观 ,并将其设置为YES
var viewIsDark = Bool() func makeViewDark() { viewIsDark = true setNeedsStatusBarAppearanceUpdate() } func makeViewLight() { viewIsDark = false setNeedsStatusBarAppearanceUpdate() } override var preferredStatusBarStyle: UIStatusBarStyle { if viewIsDark { return .lightContent } else { return .default } }
如果你想将statusBar
的颜色更改为白色,那么对于包含在UINavigationController
所有视图,请在AppDelegate
添加:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. UINavigationBar.appearance().barStyle = .blackOpaque return true }
此代码:
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
对于包含在UINavigationController
UIViewControllers
不起作用,因为编译器查找UINavigationController
的statusBarStyle
,而不是查找它包含的ViewControllers
的statusBarStyle
。
希望这有助于那些没有被接受的答案的成功!
第一步,您需要添加一行键: View controller-based status bar appearance
和值为Info.plist
文件。 之后,在你的控制器中添加2个函数,只有控制器才会生效:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) UIApplication.shared.statusBarStyle = .lightContent } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) UIApplication.shared.statusBarStyle = .default }
你也可以在故事板中做到这一点
- 在info.plist中创build一个新条目“基于视图控制器的状态栏外观”将其设置为“YES”。
- 转到故事板,然后select要更改的导航控制器。 单击故事板文档大纲部分的导航栏(故事板上的左侧面板)
- 转到右侧面板,然后单击属性部分
- 在导航栏部分,您将看到样式。 select你想要的风格(默认为黑色,黑色为白色)
您必须为每个导航控制器执行此操作。 但是,该导航控制器下的任何视图都会将所有视图的状态栏样式/颜色更改为您刚select的状态栏。 我发现这个选项更好,因为你可以立即看到你的结果,而不必在每个视图控制器中添加额外的代码行。
(在所有Swift项目中使用Xcode 8.3.3完成)
Xcode 8.3.1,Swift 3.1
-
在info.plist中创build一个新条目“基于视图控制器的状态栏外观”将其设置为“NO”。
-
打开AppDelegate.swift并在“didFinishLaunchingWithOptions”方法中添加这些行:
application.statusBarStyle = .lightContent
Swift 3
在Info.plist中添加一个名为“基于视图控制器的状态栏外观”的行,并将其值设置为“ No
。
class YourViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() UIApplication.shared.statusBarStyle = .lightContent //or .default setNeedsStatusBarAppearanceUpdate() } }
这对我有效
在plist中将View controller-based status bar
外观设置为NO ,然后在UIViewController
viewDidAppear
只是添加了以下行
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
迅速3
如果在Info.plist中,基于视图控制器的状态栏外观= YES
然后对所有的NavigationController使用这个扩展
extension UINavigationController { override open var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } }
如果没有UINavigationController并且只有UIViewController,则使用下面的代码:
extension UIViewController { override open var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } }
重要的说明
理解定制状态栏外观的两种方法是非常重要的。 他们是不同的,不应该混合。
第一种方法 – 整个应用程序的一种颜色
在info.plist中,您可以find或创build一个名为的密钥
View controller-based status bar appearance
并将其设置为NO 。
它能做什么? 它基本上build立一个设置,说在你的应用程序中,状态栏的外观不是由每个视图控制器单独定义的 。 这是非常重要的理解。 这意味着对于所有屏幕,您都有统一的整个应用程序设置。 有两种设置: default
,这是白色背景上的黑色文本,或lightContent
,这是黑色背景上的白色文本。
要设置其中的一个( 所有屏幕的一个设置 ):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { application.statusBarStyle = .lightContent // .default return true }
这样你就不需要在每个视图控制器上重新build立这个设置。 但是,你总是可以用这种方法来主动改变外观。
第二种方法 – 每个视图控制器的颜色
这是相反的。 要使其工作,请进入info.plist并设置
View controller-based status bar appearance
到YES
这样,每当一个新的视图控制器打开时,如果你在每个需要的UIViewController
实例中插入这个实现,状态栏的风格是单独设置的:
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent // .default }
你有和第一个相同的地方,为状态栏设置黑色或者浅色的风格,为每个视图控制器设置个人风格。
第三种方法 – 黑客!
有一个黑客可以直接访问状态栏:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if let statusbar = value(forKey: "statusBar") as? UIView { statusbar.backgroundColor = UIColor.blue } return true }
为什么黑客? 如果您需要黑色或白色以外的状态栏颜色,则使用未公开的API。 您使用KVC获取statusBar
对象并设置其背景颜色。 你得到这样的对象是UIStatusBar
,它是从UIView
派生,因此自然支持backgroundColor
属性。 这是肮脏的,不合法的方式,但到目前为止,这是设置状态栏的自定义颜色的唯一方法(不考虑UINavigationBar
方法,它允许自定义导航条+状态栏的外观)。 这可能会导致你的应用程序被拒绝。 但是,也许你是幸运的。 如果你是,在某些复杂的情况下(比如嵌套的,子导航和视图控制器的层次结构),这几乎是唯一的,或者至less是定制状态栏外观的麻烦方法(例如,使其透明)
总而言之,为了一劳永逸地设置状态栏风格,您需要将上述标志设置为NO ,以便状态栏不会每个视图控制器初始化其风格。 或者,要让每个视图控制器定义状态栏外观,请将该标志设置为YES并按照上述说明进行操作。
Swift 4.0请使用此代码在“didFinishLaunchingWithOptions launchOptions:”Appdelegate类
UIApplication.shared.statusBarStyle = .lightContent let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView if statusBar.responds(to: #selector(setter: UIView.backgroundColor)){ statusBar.backgroundColor = UIColor.black }
我得到:
重写var必须与其封闭types一样可访问
这是通过添加public
来解决的:
override public var preferredStatusBarStyle: UIStatusBarStyle { get { return .lightContent } }
在Swift3 iOS10上。
对于目标C,只需在应用程序didFinishLaunch方法中添加此行即可
UIApplication.sharedApplication.statusBarStyle = UIStatusBarStyleLightContent;
Swift 3
要在应用程序中设置相同的导航栏外观,可以在AppDelegate.swift中执行此操作:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { setupNavigationBarAppearence() return true } private func setupNavigationBarAppearence(){ let navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.isTranslucent = false //nav bar color navigationBarAppearace.barTintColor = UIColor.primaryColor() //SETS navbar title string to white navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] //Makes the batery icon an any other icon of the device to white. navigationBarAppearace.barStyle = .black }