ios 在单个视图中隐藏 iPhone 上的状态栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8326804/
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
Hide the status bar on iPhone on a single view?
提问by Adam Waite
I want to show the status bar in my app in all views but one. I have tried modifying the 'status bar is initially hidden' in the plist, i have tried:
我想在我的应用程序中的所有视图中显示状态栏,但只有一个。我试过修改 plist 中的“状态栏最初是隐藏的”,我试过:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
That hides the bar but leaves an ugly blue box where the status bar was (which isn't part of my view, there's nothing blue on there).
这隐藏了栏,但在状态栏所在的位置留下了一个丑陋的蓝色框(这不是我认为的一部分,那里没有任何蓝色)。
I have also tried altering the layout wants full screen and status bar settings in the 'interface builder' bit of Xcode 4.2.
我还尝试在 Xcode 4.2 的“界面构建器”位中更改布局需要全屏和状态栏设置。
Any suggestions?
有什么建议?
EDIT - SORT OF SOLUTION:
编辑 - 解决方案的排序:
I have done it by including:
我已经做到了,包括:
-(void)viewWillDisappear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
-(void)viewDidAppear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
on every single page that I want the status bar to be on.
在我希望状态栏打开的每个页面上。
It still looks choppy and rubbish because the tab bar appears and reappears each time you switch a view. But i've had enough, worked on this stupid problem for about 5 hours now so this will have to do.
它看起来仍然断断续续和垃圾,因为每次切换视图时标签栏都会出现并重新出现。但是我已经受够了,现在在这个愚蠢的问题上工作了大约 5 个小时,所以必须这样做。
SECOND EDIT -
第二次编辑 -
Fixed the choppyness by including setStatusBarHidden=NO in viewWillAppears. God knows how everything works but it does.
通过在 viewWillAppears 中包含 setStatusBarHidden=NO 修复了断断续续的问题。天知道一切是如何运作的,但它确实如此。
回答by Kartik
Try This one It will Run perfectly..
试试这个它会完美运行..
[[UIApplication sharedApplication] setStatusBarHidden:YES];
And in XIB set none option for the status bar.
并在 XIB 中为状态栏设置 none 选项。
for iOS 7.
适用于 iOS 7。
Go to info.plist and add two attributes if not present. set "Status bar is initially hidden" to "YES" and set "UIViewControllerBasedStatusBarAppearance" to "NO". This will hide status bar for your app.
转到 info.plist 并添加两个属性(如果不存在)。将“状态栏最初是隐藏的”设置为“是”并将“UIViewControllerBasedStatusBarAppearance”设置为“否”。这将隐藏您的应用程序的状态栏。
回答by neoneye
#pragma mark - Hide statusbar
-(void)hideStatusBar {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
// iOS 7.0 or later
[self setNeedsStatusBarAppearanceUpdate];
#else
// less than 7
[[UIApplication sharedApplication] setStatusBarHidden:YES];
#endif
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
回答by Adam Waite
If there is anyone looking for a solution where the above solution does not work (and there is still an annoying blue 20px gap at the top), try putting this in the viewWillAppear on the implementation file of the view controller that you would like the status bar to be hidden.
如果有人正在寻找上述解决方案不起作用的解决方案(并且顶部仍然存在令人讨厌的蓝色 20px 间隙),请尝试将其放在视图控制器的实现文件上的 viewWillAppear 中,您希望获得该状态要隐藏的栏。
self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);
That literally took me 12 hours or so to fix, and that was the solution, so now i'm spreading the word in case anyone else has this annoying issue.
这实际上花了我 12 个小时左右的时间来修复,这就是解决方案,所以现在我正在传播这个词,以防其他人遇到这个烦人的问题。
回答by WillShakes
Kartik's solution worked for me.
Kartik 的解决方案对我有用。
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
I added this to viewWillAppear:
instance method.
我将此添加到viewWillAppear:
实例方法中。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.co.uk"]]];
}
And I spent ages on this too. Using Xcode 4.2, iOS5 sim.
我也花了很多时间在这上面。使用 Xcode 4.2,iOS5 sim。
But when I first implemented it, the annoying "space" at the top was there. I selected the View Controller in the storyboard and set the properties as follows: Size: Full Screen StatusBar: None everything else inferred.
但是当我第一次实施它时,顶部令人讨厌的“空间”就在那里。我在情节提要中选择了视图控制器并按如下方式设置属性: 大小:全屏状态栏:无其他所有推断。
I checked wants full screen.
我检查了要全屏。
Voila, it all worked fine.
瞧,一切都很好。
回答by Patricio Bravo
I know this is an old question but none of this answers works for me, so this is how it works for me to hide the status bar in a single viewController
我知道这是一个老问题,但这些答案都不适合我,所以这就是我在单个 viewController 中隐藏状态栏的工作方式
First in your parentViewController you have to set:
首先在你的 parentViewController 中你必须设置:
- (UIViewController *)childViewControllerForStatusBarHidden {
if ( hideStatusBarViewController ) {
return hideStatusBarViewController;
}
return nil
}
It only returns the child view controller when its created otherwise nil is the default. When you add your hideStatusBarViewController you must call
它仅在创建时返回子视图控制器,否则 nil 是默认值。当你添加你的 hideStatusBarViewController 时,你必须调用
[self setNeedsStatusBarAppearanceUpdate];
on the parentViewController, this function forces to read childViewControllerForStatusBarHidden. Finally in hideStatusBarViewController you must set
在 parentViewController 上,此函数强制读取 childViewControllerForStatusBarHidden。最后在 hideStatusBarViewController 中,您必须设置
- (BOOL)prefersStatusBarHidden {
return YES;
}
Its the only solution that works for me. I hope it help somebody.
它是唯一对我有用的解决方案。我希望它可以帮助某人。
回答by Sihad Begovic
This is solution if you want to hide status bar on a single view
如果您想在单个视图上隐藏状态栏,这是解决方案
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.view sizeToFit];
}
回答by Fran Sevillano
I would suggest you a different approach: insert that view onto the application's window:
我建议您采用不同的方法:将该视图插入到应用程序的窗口中:
YourUIAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window insertSubview:self.yourView atIndex:([[appDelegate.window subviews]count])];
That way it will show over the status bar
这样它就会显示在状态栏上
I hope it helps you
我希望它能帮助你
回答by Louie
Here is a snippet of code that might help. When the view loads show the status bar, when you leave the view hide it again.
这是可能有帮助的一段代码。当视图加载时显示状态栏,当您离开视图时再次隐藏它。
-(void)viewWillAppear:(BOOL)animated {
if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
else
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated {
if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
else
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
回答by David
The [[UIApplication sharedApplication] setStatusBarHidden:BOOL];
is enough, but:
该[[UIApplication sharedApplication] setStatusBarHidden:BOOL];
是够了,但是:
Remember to set it back to
NO
before leaving the view or the page you return to will likely have the Nav Bar under the status bar.Make sure that you set both status bar hide and show in the view in which you want the status bar hidden. I did the switch off in the
viewDidLoad
method, and - crucially- the switch back on in theviewWillDisappear:animated
method. Any later and you're in for trouble.
请记住
NO
在离开视图之前将其设置回,否则您返回的页面可能会在状态栏下方显示导航栏。确保在要隐藏状态栏的视图中设置状态栏隐藏和显示。我在
viewDidLoad
方法中关闭了开关,并且 -至关重要的是- 在viewWillDisappear:animated
方法中重新打开。再晚一点,你就有麻烦了。