ios 未调用首选状态栏隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33541525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:11:32  来源:igfitidea点击:

prefersStatusBarHidden not called

iosstatusbar

提问by Md1079

I have a UITabViewController-> UINavigationController-> UIViewControllerand want to hide and unhide the statusBar. when I call setNeedsStatusBarAppearanceUpdate()the method prefersStatusBarHiddenis not called.

我有一个UITabViewController-> UINavigationController->UIViewController并且想要隐藏和取消隐藏状态栏。当我调用setNeedsStatusBarAppearanceUpdate()该方法时prefersStatusBarHidden没有调用。

func fadeOutStatusBar (notification: NSNotification) {
    statusBarHidden = true
    self.setNeedsStatusBarAppearanceUpdate()
}

func fadeInStatusBar (notification: NSNotification) {
    statusBarHidden = false
    self.setNeedsStatusBarAppearanceUpdate()
}

override func prefersStatusBarHidden() -> Bool {
    return statusBarHidden
}

采纳答案by Md1079

Figured it out. in info.plist file: view controller-status bar appearance should be set to YES

弄清楚了。在 info.plist 文件中:视图控制器状态栏外观应设置为 YES

回答by Nghia Luong

Firstly, View controller-based status bar appearancein the .plistfile must be set to YES.

首先,View controller-based status bar appearance.plist文件中必须设置为YES

  • If you want status bar to be hidden in the wholeapp:
  • 如果您希望在整个应用程序中隐藏状态栏:

For Objective-C:

对于 Objective-C:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [application setStatusBarHidden:YES];

    return YES;
}

For Swift:

对于斯威夫特:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
    application.statusBarHidden = true

    return true
}
  • If you want status bar is disappeared in Specify View Controller, in .mfile, just implement:
  • 如果你想让状态栏在指定视图控制器中消失,在.m文件中,只需实现:

For Objective-C:

对于 Objective-C:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

For Swift:

对于斯威夫特:

override func prefersStatusBarHidden() -> Bool {
    return true
}

回答by Barrett

For swift 3, first, make sure that View controller-based status bar appearanceis set to YESin your Infoplist file

对于 swift 3,首先,确保在你的plist 文件中View controller-based status bar appearance设置为YESInfo

screenshot

截屏

And then just add this to your view controller:

然后将其添加到您的视图控制器中:

override var prefersStatusBarHidden: Bool {
    get {
        return true
    }
}

I hope this helps people in the future.

我希望这对未来的人们有所帮助。

回答by TheEye

Maybe not a solution to the OP problem, but what could also be the cause for prefersStatusBarHiddennot being called is if you have used a second window in your app delegate, eg for displaying a splash screen, and you did not hide it after the splash was shown - then that window gets the events that lead to calling these functions.

也许不是 OP 问题的解决方案,但也可能是prefersStatusBarHidden未被调用的原因是,如果您在应用程序委托中使用了第二个窗口,例如用于显示启动画面,并且您没有在启动后隐藏它显示 - 然后该窗口获取导致调用这些函数的事件。

回答by HymanySong

When we nested the UINavigationController, our AppDelegate. Window. RootViewControllerUsually we create navigationController when first will call in the navigationController childViewControllerForStatusBarHiddenfunction, because the default returns nil, then is called navigationController prefersStatusBarHiddenfunction itself, So the status bar that we set in viewController through the prefersStatusBarHiddenfunction will not be called, so it won't work. So we're going to create our own one that inherits from the NavigationController,in this subclass ChildViewControllerForStatusBarHiddenfunction.

当我们嵌套UINavigationController 时,我们的 AppDelegate. 窗户。RootViewController的通常我们创造navigationController时,首先将在navigationController调用childViewControllerForStatusBarHidden功能,因为默认返回nil,则称为navigationController prefersStatusBarHidden功能本身,所以在状态栏,我们在的viewController通过设置prefersStatusBarHidden功能不会被调用,因此它赢得了不行。所以我们要创建我们自己的继承自NavigationController 的,在这个子类ChildViewControllerForStatusBarHidden函数中。

回答by HymanySong

you can write an extension of UINavigationController that overrides its default implementation and returns the top ViewController.

您可以编写 UINavigationController 的扩展来覆盖其默认实现并返回顶部 ViewController。

  extension UINavigationController {
    override public func childViewControllerForStatusBarHidden() -> UIViewController{
       return self.topViewController
    }
   }

回答by sai Ma

If you have other windownot hidden, the method not be called. Just hide other window, it will work as you wish

如果您有其他window未隐藏的方法,则不会调用该方法。只需隐藏其他窗口,它就会如您所愿

回答by Ely

Property prefersStatusBarHiddenis being called on the root view controller of the current view controller.

prefersStatusBarHidden正在当前视图控制器的根视图控制器上调用属性。

This means that if your app is based on a UISplitViewControllerfor example, you must implement the property in a custom UISplitViewControllerclass.

这意味着,如果您的应用程序基于UISplitViewController例如,您必须在自定义UISplitViewController类中实现该属性。

回答by idej1234

For Swift 4.2 iOS 12

适用于 Swift 4.2 iOS 12

Assuming you have a ViewControllercontained within UINavigationController. Create your own Subclass of UINavigationControllerand include in it:

假设你有一个ViewController包含在UINavigationController. 创建您自己的子类UINavigationController并将其包含在其中:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Make sure info.plistsets View Controllerbased setting of status bar

确保info.plist设置View Controller基于状态栏的设置