我有一个ViewController
在UINavigationcontroller
中,但导航栏是隐藏的。当我在iOS 7上运行应用程序时,状态栏显示在我的视图顶部。有没有办法避免这种情况?
我不想写任何特定于操作系统的代码。
我尝试将 View controller-based status bar appearance
设置为NO
,但没有解决问题。
我有一个ViewController
在UINavigationcontroller
中,但导航栏是隐藏的。当我在iOS 7上运行应用程序时,状态栏显示在我的视图顶部。有没有办法避免这种情况?
我不想写任何特定于操作系统的代码。
我尝试将 View controller-based status bar appearance
设置为NO
,但没有解决问题。
Xcode 5具有,这是专门用于解决此问题的功能。在Storyboard中,我将我的视图下移20个像素以在iOS 7上呈现正确的外观,并为了使其适用于iOS 6,我将Delta y
更改为-20。
由于我的Storyboard没有使用自动布局,在iOS 6上正确调整视图高度的方法是设置Delta height
以及Delta Y
。
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
iOS 7希望你在设计应用时考虑到一个覆盖透明状态栏的设计。例如,查看新的iOS 7天气应用程序。
这是iOS 7上UIViewController的默认行为。视图会全屏显示,意味着状态栏会覆盖您视图的顶部。
如果您在UINavigationController中有一个UIViewController并且navigationBar可见,则可以在您的viewDidLoad中使用以下代码或为navigationBar设置背景图片来解决此问题。
self.edgesForExtendedLayout = UIRectEdgeNone;
如果你隐藏了navigationBar,那么你需要将所有的UIView元素上移20个点。我没有看到其他解决方法。使用自动布局会有所帮助。NSUInteger DeviceSystemMajorVersion() {
static NSUInteger _deviceSystemMajorVersion = -1;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
_deviceSystemMajorVersion = [[systemVersion componentsSeparatedByString:@"."][0] intValue];
});
return _deviceSystemMajorVersion;
}
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
,而不是获取、解析和比较系统版本。 - Sebastian Hojas这是我自己制作的仅有效的解决方案。
这是我的UIViewController子类:https://github.com/comonitos/ios7_overlaping
1 从UIViewController中创建子类
2 将你的window.rootViewController从该类中创建子类。
3 就这样!
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect screen = [[UIScreen mainScreen] bounds];
if (self.navigationController) {
CGRect frame = self.navigationController.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.navigationController.view.frame = frame;
} else {
if ([self respondsToSelector: @selector(containerView)]) {
UIView *containerView = (UIView *)[self performSelector: @selector(containerView)];
CGRect frame = containerView.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
containerView.frame = frame;
} else {
CGRect frame = self.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.view.frame = frame;
}
}
}
}
在 [self.window makeKeyAndVisible]; 代码行的下一行添加以下内容,以使您的状态栏变为白色。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
-(void)viewWillLayoutSubviews{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.view.clipsToBounds = YES;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = 0.0;
if(UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
screenHeight = screenRect.size.height;
else
screenHeight = screenRect.size.width;
CGRect screenFrame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
CGRect viewFrame1 = [self.view convertRect:self.view.frame toView:nil];
if (!CGRectEqualToRect(screenFrame, viewFrame1))
{
self.view.frame = screenFrame;
self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
}
}
在 plist 中添加键值对 --- View controller-based status bar appearance : NO
要在iOS7中隐藏状态栏,请按照以下简单步骤:
在Xcode中进入“资源
”文件夹并打开“(应用程序名称)-Info.plist文件
”。
View controller based status bar appearance
”键,并将其值设置为“NO
”Status bar is initially hidden
”键,并将其值设置为“YES
”如果这些键不存在,则可以通过选择顶部的“信息属性列表
”并点击+图标来添加它们。
xib
,一个非常简单的实现方法是将所有子视图都封装在具有调整大小标志(这些标志已经用于3.5英寸和4英寸兼容性)的容器视图中,以便视图层次结构看起来像这样:
viewDidLoad
中,可以做如下处理:- (void)viewDidLoad
{
[super viewDidLoad];
// initializations
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) // only for iOS 7 and above
{
CGRect frame = containerView.frame;
frame.origin.y += 20;
frame.size.height -= 20;
containerView.frame = frame;
}
}
containerView
之外,并让它覆盖整个屏幕。-(BOOL) prefersStatusBarHidden
{
return YES;
}
该链接提供了一个关于如何在Java中实现基于内存的缓存的答案。简单来说,它使用了LinkedHashMap,并覆盖了removeEldestEntry方法以移除最久未被访问的元素,从而避免了内存溢出。
导航栏:
编写以下代码:
self.navigationController.navigationBar.translucent = NO;
这个技巧对我很有帮助。