在iOS 7中,UIStatusBar被设计成这样,它与视图合并:
(GUI由Tina tavvar设计)
It is cool, but it will somewhat mess up your view when you have something at the top part of your view, and it becomes overlapped with the status bar.
Is there a simple solution (such as setting a property in info.plist) that can change the way it works [not overlapping] back to how it is in iOS6?
I know a more straightforward solution is to have self.view.center.x + 20 points for every single view controller, but changing them will screw other dimensions up (having a different self.view.center.x can cause problem to custom segues, etc.) and suddenly it turns into a tedious job that is best to be avoided.
I'll really be glad if someone can provide me an one-liner solution for this.
附注:我知道我可以隐藏状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
在didFinishLaunchingWithOptions方法中,但这是一个变通方法,一个避免问题的快捷方式,所以我不认为这是一个真正的解决方案。
试试这个简单的方法....
步骤1:在单个viewController中更改
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
步骤2:更改整个应用程序
info.plist
----> Status Bar Style
--->UIStatusBarStyle to UIStatusBarStyleBlackOpaque
第三步:在每个viewWillAppear中添加这个来调整iOS7的状态栏高度
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7) {
CGRect frame = [UIScreen mainScreen].bounds;
frame.origin.y+=20.0;
frame.size.height-= 20.0;
self.view.frame = frame;
[self.view layoutIfNeeded];
}
我的解决方案是在iOS 7上,在窗口顶部添加一个高度为20点的UIView。
然后我在AppDelegate类中创建了一个方法来显示/隐藏“固态”状态栏背景。在应用程序:didFinishLaunchingWithOptions::
// ...
// Add a status bar background
self.statusBarBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 20.0f)];
self.statusBarBackground.backgroundColor = [UIColor blackColor];
self.statusBarBackground.alpha = 0.0;
self.statusBarBackground.userInteractionEnabled = NO;
self.statusBarBackground.layer.zPosition = 999; // Position its layer over all other views
[self.window addSubview:self.statusBarBackground];
// ...
return YES;
然后我创建了一个方法来淡入/淡出黑色状态栏背景:
- (void) showSolidStatusBar:(BOOL) solidStatusBar
{
[UIView animateWithDuration:0.3f animations:^{
if(solidStatusBar)
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.statusBarBackground.alpha = 1.0f;
}
else
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
self.statusBarBackground.alpha = 0.0f;
}
}];
}
我现在要做的就是在需要时调用[appDelegate showSolidStatusBar:YES]。
因为使用presentViewController:animated:completion:把window.rootViewController弄乱了。因此,我必须找到一个不同的方法来解决这个问题。我终于让它与模态和旋转工作通过子类化我的rootViewController的UIView。
.h
@interface RootView : UIView
@end
.m
@implementation RootView
-(void)setFrame:(CGRect)frame
{
if (self.superview && self.superview != self.window)
{
frame = self.superview.bounds;
frame.origin.y += 20.f;
frame.size.height -= 20.f;
}
else
{
frame = [UIScreen mainScreen].applicationFrame;
}
[super setFrame:frame];
}
- (void)layoutSubviews
{
self.frame = self.frame;
[super layoutSubviews];
}
@end
现在你有了iOS7动画的强大解决方案。