ios IOS7中的状态栏和导航栏问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18980925/
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
Status bar and navigation bar issue in IOS7
提问by Ganapathy
I am migrating my application to iOS 7. For handing the status bar issue I have added this code
我正在将我的应用程序迁移到 iOS 7。为了处理状态栏问题,我添加了此代码
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
CGRect frame = self.navigationController.view.frame;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
frame.origin.y = 20;
}
else
{
frame.origin.x = 20;
}
[self.navigationController.view setFrame:frame];
}
This is working fine in normal case. If I am changing orientation (app supports only landscape orientation) or presenting any view controller and dismissing model view controller my view controller alignment changed. The status bar again overlaps my view controller. This piece of code is not working at all. Please guide me to fix this status bar issue.
这在正常情况下工作正常。如果我正在更改方向(应用程序仅支持横向)或呈现任何视图控制器并关闭模型视图控制器,则我的视图控制器对齐方式发生了变化。状态栏再次与我的视图控制器重叠。这段代码根本不起作用。请指导我解决此状态栏问题。
Case 2: This is how I am presenting my view controller
案例 2:这就是我展示我的视图控制器的方式
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
reader.supportedOrientationsMask = ZBarOrientationMaskLandscape;
else
reader.supportedOrientationsMask = ZBarOrientationMaskPortrait;
[self presentModalViewController:reader animated:YES];
Ref:
参考:
Thanks in advance.
提前致谢。
回答by Ganapathy
Fix for status bar issue in IOS 7
修复 IOS 7 状态栏问题
Finally i fixed the status bar over lap issue using the delta value property in xcode5. First i have increased origin - y 20pxl to all the controller used in the Xib(it seams to be working fine only in IOS 7), after that i set the delta value for all the view controller origin -y to -20 it works fine in both ios 6 and IOS 7.
最后,我使用 xcode5 中的 delta 值属性修复了状态栏重叠问题。首先,我将原点 - y 20pxl 增加到 Xib 中使用的所有控制器(它只能在 IOS 7 中正常工作),之后我将所有视图控制器的 delta 值设置为 -20,它工作正常在 ios 6 和 IOS 7 中。
Steps to do that.
这样做的步骤。
Xcode 5 provide preview option to view the appearance of the xib in different view based on the OS version.
Xcode 5 提供了预览选项,可以根据操作系统版本在不同的视图中查看 xib 的外观。
choose preview option from assistant editor
从助理编辑器中选择预览选项
click assistant editor
点击助理编辑
and choose preview option to preview selected view controller in different version.
并选择预览选项以预览不同版本的选定视图控制器。
view controller view preview option.
视图控制器视图预览选项。
in preview you can find the toggle option to preview view in different version. In preview u can feel the status bar issue clearly if its not fixed properly by toggle the version.
在预览中,您可以找到切换选项以预览不同版本的视图。在预览中,如果通过切换版本没有正确修复状态栏问题,您可以清楚地感觉到状态栏问题。
Three steps to fix the status bar issue:
step 1:Make sure the view target us 7.0 and later in File inspector.
修复状态栏问题的三个步骤:
第 1 步:确保文件检查器中的视图针对我们 7.0 及更高版本。
Step 2 : Increase the origin - y with 20 pixel(exactly the size of the status bar) for all the controls added in the view controller.
第 2 步:为视图控制器中添加的所有控件增加原点 - y 20 像素(正好是状态栏的大小)。
Step 3 : Set the delta value of origin y to -20 for all the controlsthen only it will adjust automatically based on the version. Use preview now and feel the differ that the controls automatically adjust because of the delta value.
步骤3:将所有控件的原点y的delta值设置为-20,然后它会根据版本自动调整。现在使用预览,感受控件因增量值而自动调整的不同。
Once the status bar issue fixed, issue while presenting the model view (ZbarSDk controller) is also fixed automatically.
状态栏问题修复后,显示模型视图(ZbarSDk 控制器)时的问题也会自动修复。
Preview screen :
预览画面:
回答by Vizllx
I am late for this Answer, but i just want to share what i did, which is basically
the easiest solution
我迟到了这个答案,但我只想分享我所做的,这基本上
是最简单的解决方案
Firstof all-> Go to your info.plist File
and add Status Bar Style->Transparent Black Style(Alpha of 0.5)
首先-> 转到您的info.plist File
并添加状态栏样式-> 透明黑色样式(Alpha 为 0.5)
Now ,here it Goes:-
现在,它来了:-
Add this code in your AppDelegate.m
在您的 AppDelegate.m 中添加此代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Whatever your code goes here
if(kDeviceiPad){
//adding status bar for IOS7 ipad
if (IS_IOS7) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 1024, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; //change this to match your navigation bar
[self.window.rootViewController.view addSubview:addStatusBar];
}
}
else{
//adding status bar for IOS7 iphone
if (IS_IOS7) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; //You can give your own color pattern
[self.window.rootViewController.view addSubview:addStatusBar];
}
return YES;
}
回答by John
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
self.window.clipsToBounds =YES;
self.window.frame =CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}
[self.window makeKeyAndVisible];
return YES;
}
set the following to info.plist
将以下内容设置为 info.plist
View controller-based status bar appearance = NO;
基于视图控制器的状态栏外观 = NO;
回答by Mandeep Pasbola
To hide status bar in ios7 follow these simple steps :
要在 ios7 中隐藏状态栏,请按照以下简单步骤操作:
In Xcode goto "Resources
" folder and open "(app name)-Info.plist file
".
在 Xcode 中转到“ Resources
”文件夹并打开“ (app name)-Info.plist file
”。
- check for "
View controller based status bar appearance
" key and set its value "NO
" - check for "
Status bar is initially hidden
" key and set its value "YES
"
- 检查“
View controller based status bar appearance
”键并设置其值“NO
” - 检查“
Status bar is initially hidden
”键并设置其值“YES
”
If the keys are not there then you can add it by selecting "information property list
" at top and click +icon
如果键不存在,那么您可以通过选择information property list
顶部的“ ”并单击+图标来添加它
回答by sinh99
Hear we can do this for all views at once
听说我们可以一次为所有视图执行此操作
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Notification for the orientaiton change
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidChangeStatusBarOrientation:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
// Window framing changes condition for iOS7 or greater
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
statusBarBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.window.frame.size.width, 20)];//statusBarBackgroundView is normal uiview
statusBarBackgroundView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.730];
[self.window addSubview:statusBarBackgroundView];
self.window.bounds = CGRectMake(0, -20, self.window.frame.size.width, self.window.frame.size.height);
}
// Window framing changes condition for iOS7 or greater
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And While we are using orientation we can add below method in app delegate to set it via orientation.
当我们使用方向时,我们可以在应用程序委托中添加以下方法以通过方向设置它。
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
statusBarBackgroundView.hidden = YES;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
int width = [[UIScreen mainScreen] bounds].size.width;
int height = [[UIScreen mainScreen] bounds].size.height;
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
self.window.bounds = CGRectMake(-20,0,width,height);
statusBarBackgroundView.frame = CGRectMake(-20, 0, 20, height);
break;
case UIInterfaceOrientationLandscapeRight:
self.window.bounds = CGRectMake(20,0,width,height);
statusBarBackgroundView.frame = CGRectMake(320, 0, 20, height);
break;
case UIInterfaceOrientationPortraitUpsideDown:
statusBarBackgroundView.frame = CGRectMake(0, 568, width, 20);
self.window.bounds = CGRectMake(0, 20, width, height);
break;
default:
statusBarBackgroundView.frame = CGRectMake(0, -20, width, 20);
self.window.bounds = CGRectMake(0, -20, width, height);
break;
}
statusBarBackgroundView.hidden = NO;
}
}
You should Add below navigation controller category for it
您应该为它添加以下导航控制器类别
.h
。H
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface UINavigationController (iOS6fix)
@end
.m
.m
#import "UINavigationController+iOS6fix.h"
@implementation UINavigationController (iOS6fix)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
回答by Brad
MUCH MUCH MUCHsimpler answer:
很多很多MUCH简单的答案:
Align the top of your view to the "top layout guide", but control-dragging "Top Layout Guide" to your view and setting the "vertical" constraint. See this answerfor a picture reference.
将视图的顶部与“顶部布局指南”对齐,但将“顶部布局指南”控制拖动到您的视图并设置“垂直”约束。请参阅此答案以获取图片参考。
The way it works is - the "Top Layout Guide" will automagically ajust itself for when the status bar is or is not there, and it will all work - no coding required!
它的工作方式是——“顶部布局指南”将自动调整状态栏何时出现或不存在,并且一切都会起作用——不需要编码!
P.S. In this particularexample, the background showing through at the bottom should also be resolved by setting an appropriate vertical constraint of the view's bottom, to it's superview, or whatever...
PS 在这个特定的例子中,在底部显示的背景也应该通过设置视图底部的适当垂直约束来解决,它是超级视图,或者其他什么......
回答by Amit Shelgaonkar
just set the following code in viewWillAppear
.
只需在viewWillAppear
.
if ([[[UIDevice currentDevice] systemVersion] floatValue]<= 7) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
回答by Bruno Grieder
With Salesforce SDK 2.1 (Cordova 2.3.0) we had to do the following to get the status bar appear on the initial load of the App and coming back from the background (iPhone and iPad):
使用 Salesforce SDK 2.1 (Cordova 2.3.0),我们必须执行以下操作才能在应用程序的初始加载时显示状态栏并从后台(iPhone 和 iPad)返回:
Contrarily to other solutions posted here, this one seems to survive rotation of the device.
与此处发布的其他解决方案相反,这个解决方案似乎可以在设备旋转时幸存下来。
1-Create a category of theSFHybridViewController
1-创建一个类别SFHybridViewController
#import "SFHybridViewController+Amalto.h"
@implementation SFHybridViewController (Amalto)
- (void)viewWillAppear:(BOOL)animated
{
//Lower screen 20px on ios 7
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = self.view.bounds;
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
}
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = self.view.bounds;
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
}
[super viewDidLoad];
}
@end
2-Add to AppDelegate.m
imports
2-添加到AppDelegate.m
进口
#import "SFHybridViewController+Amalto.h"
3-Inject at the end of of method didFinishLaunchingWithOptions
of AppDelegate
3-进样在方法结束时didFinishLaunchingWithOptions
的AppDelegate
//Make the status bar appear
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
4-Add to App-Info.plist
the property
4-添加到App-Info.plist
属性
View controller-based status bar appearance
with value NO
View controller-based status bar appearance
有价值 NO
回答by user2230971
i solved this by using below code
我用下面的代码解决了这个问题
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(landScape mode)
if ([UIDevice currentDevice].systemVersion.floatValue>=7) {
CGRect frame = self.window.frame;
frame.size.width -= 20.0f;
frame.origin.x+= 20.0f;
self.window.frame = frame;
}
if(portrait)
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
CGRect frame = self.window.frame;
frame.origin.y += 20.0f;
frame.size.height -= 20.0f;
self.window.frame = frame;
}
return YES;
}
回答by Mubin Shaikh
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
if (_kisiOS7)
{
[[UINavigationBar appearance] setBarTintColor:_kColorFromHEX(@"#011C47")];
}
else
{
[[UINavigationBar appearance] setBackgroundColor:_kColorFromHEX(@"#011C47")];
[[UINavigationBar appearance] setTintColor:_kColorFromHEX(@"#011C47")];
}