Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

objective c - Under iOS 7, how do I hide and show status bar on the fly (whenever I want to)

Say a user is in a View Controller and wants to enter a "full screen" type mode where the status bar is hidden, under iOS 6 it was a simple method call to hide/show the status bar, but no matter what it seems to persist under iOS 7.

I've seen solutions like this:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

But that doesn't allow it to be toggled at runtime. (It doesn't accept any arguments.)

In my info.plist I have View controller-based status bar appearance set to NO.

I'm at wits end. How do I hide it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Swift 4

show:

(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = false

hide:

(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = true



Objective-c

Well here's one way of doing this:

in myViewController.h

@interface myViewController : UIViewController {
    BOOL shouldHideStatusBar;
}

Then in myViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    shouldHideStatusBar = YES;
}

- (BOOL)prefersStatusBarHidden {
    return shouldHideStatusBar;
}

and let's say when I touch the screen it should show the status bar now. You'll need to call: setNeedsStatusBarAppearanceUpdate specifically to get this working and then a switch (bool in this case) to show/hide.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    shouldHideStatusBar = (shouldHideStatusBar)? NO: YES;
    [self setNeedsStatusBarAppearanceUpdate];
}

setNeedsStatusBarAppearanceUpdate

This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.

prefersStatusBarHidden:

Return Value A Boolean value of YES specifies the status bar should be hidden. Default value is NO.

Discussion If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdate method.

To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHidden method.


If you plan on your app working with iOS 6 as well might want to look at this post


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...