How can I detect UIStatusBar hide and show?

后端 未结 4 1068
一个人的身影
一个人的身影 2021-01-18 12:00

I\'m trying to detect hidden and show of iPhone\'s UIStatusBar but failed. Are there any solution can help me, like KVO or something else?

相关标签:
4条回答
  • 2021-01-18 12:22

    From iOS 11 and up you can subclass the UIView of the view controller and override safeAreaInsetsDidChange:

    override func safeAreaInsetsDidChange() {
      super.safeAreaInsetsDidChange()
      // adapt your view
    }
    

    Your view must share the top rect with the status bar for this to work. (But if it doesn't, you probably wouldn't need to detect changes anyway).

    0 讨论(0)
  • 2021-01-18 12:26

    iOS 13

    Since isStatusBarHidden and setStatusBarHidden are deprecated from iOS 13 you can check visibility of the status bar with UIStatusBarManager.isStatusBarHidden and Timer since it is not supported by KVO:

    timer = Timer.scheduledTimer(withTimeInterval: 0.3, repeats: true) { timer in
        if let statusBarManager = UIApplication.shared.delegate?.window??.windowScene?.statusBarManager {
            print(statusBarManager.isStatusBarHidden)
        }
    }
    
    0 讨论(0)
  • 2021-01-18 12:30

    You can observe the statusBarHidden property of the shared UIApplication instance.

    Simple example:

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        // Do something here...
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [[UIApplication sharedApplication] addObserver:self forKeyPath:@"statusBarHidden" options:NSKeyValueObservingOptionNew context:NULL];
        [[UIApplication sharedApplication] setStatusBarHidden:YES]; // Will notify the observer about the change
    }
    
    0 讨论(0)
  • 2021-01-18 12:30

    In UIApplication class there is a property statusBarHidden...this tell status bar hidden or not...if it return YES mean status bar is hidden...try this.

    0 讨论(0)
提交回复
热议问题