仅在一个ViewController中显示/隐藏状态栏,目标C,iOS

时间:2016-10-24 16:32:24

标签: ios objective-c statusbar

我希望状态栏显示在viewWillAppear()中并在viewController的viewWillDisappear()中消失

我正在使用

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];

但在iOS 9.0中已弃用

我正在使用

UIApplication.shared.isStatusBarHidden = false

在swift中,但在目标C中,这只是值......

prefersStatusBarHidden对我来说也不起作用,因为我需要在viewWillDisappear()函数中隐藏状态栏

-(BOOL)prefersStatusBarHidden{
    return YES;
}

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:4)

对于每个视图控制器,您想要更改状态栏的可见性,您需要覆盖prefersStatusBarHidden。为此,实际上工作,您必须在项目的Info.plist中添加以下键/值:

<强>键: View controller-based status bar appearance

值: YES

要控制viewWillAppearviewWillDisappear中状态栏的可见性,您可以执行以下操作:

var statusBarHidden = false

override func viewWillAppear() {
    super.viewWillAppear()
    statusBarHidden = false
    self.setNeedsStatusBarAppearanceUpdate()
}

override func viewWillDisappear() {
    super.viewWillDisappear()
    statusBarHidden = true
    self.setNeedsStatusBarAppearanceUpdate()
}

override var prefersStatusBarHidden: Bool {
    return statusBarHidden
}

答案 1 :(得分:0)

对于Swift 3,

override var prefersStatusBarHidden: Bool{
        return true
    }

并添加viewDidLoad()

self.modalPresentationCapturesStatusBarAppearance = true

答案 2 :(得分:0)

为Objective-C编写示例(@dennykim已经为SWIFT编写了相同的代码)

  1. 为BOOL创建属性

@property(nonatomic,assign)BOOL statusBarHidden;

  1. 在info.plist中设置查看基于控制器的状态栏外观 ==
  2. 转到ViewController并编写下一个代码:

-(void)viewWillAppear:(BOOL)动画{

[super viewWillAppear:animated];

self.statusBarHidden = TRUE;
[self setNeedsStatusBarAppearanceUpdate];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    self.statusBarHidden = FALSE;
    [self setNeedsStatusBarAppearanceUpdate];   
}

- (BOOL)prefersStatusBarHidden {
    return self.statusBarHidden;
}