[英]Status bar and navigation bar issue in IOS7
我正在将我的应用程序迁移到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];
}
这在正常情况下工作正常。 如果我正在更改方向(应用程序仅支持横向方向)或呈现任何视图控制器并关闭模型视图控制器,我的视图控制器对齐方式已更改。 状态栏再次与我的视图控制器重叠。 这段代码根本不起作用。 请指导我修复此状态栏问题。
案例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];
参考:
提前致谢。
修复了IOS 7中的状态栏问题
最后,我使用xcode5中的delta值属性修复了状态栏。 首先我增加了原点 - 对于Xib中使用的所有控制器都是20pxl (它的接缝仅在IOS 7中工作正常),之后我将所有视图控制器原点的delta值设置为-y它工作正常在ios 6和IOS 7中 。
这样做的步骤。
Xcode 5提供预览选项,可根据操作系统版本查看不同视图中xib的外观。
从助理编辑器中选择预览选项
点击助理编辑器
并选择预览选项以预览不同版本的所选视图控制器。
查看控制器视图预览选项。
在预览中,您可以找到切换选项以预览不同版本的视图。 在预览中,如果通过切换版本未正确修复状态栏,则可以清楚地感觉到状态栏问题。
修复状态栏问题的三个步骤: 步骤1:确保视图在文件检查器中以7.0及更高版本为目标 。
步骤2:增加原点 - y为视图控制器中添加的所有控件的20像素 (与状态栏的大小完全相同)。
步骤3:将所有控件的原点y的delta值设置为-20,然后仅根据版本自动调整。 现在使用预览,并感受控件因delta值而自动调整的差异。
状态栏问题解决后,在显示模型视图(ZbarSDk控制器)时发出的问题也会自动修复。
预览屏幕:
我这个答案迟到了,但我只想分享我所做的,基本上就是这样
最简单的解决方案
首先 - >转到info.plist File
并添加状态栏样式 - >透明黑色样式(0.5的Alpha)
现在,它来了: -
在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;
}
- (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;
}
将以下内容设置为info.plist
查看基于控制器的状态栏外观= NO;
听说我们可以同时为所有观点做到这一点
- (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;
}
虽然我们使用方向,但我们可以在app delegate中添加以下方法来通过方向设置它。
- (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;
}
}
您应该为它添加以下导航控制器类别
。H
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface UINavigationController (iOS6fix)
@end
.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
要在ios7中隐藏状态栏,请按照以下简单步骤操作:
在Xcode中转到“ Resources
”文件夹并打开“ (app name)-Info.plist file
”。
View controller based status bar appearance
”键并将其值设置为“ NO
” Status bar is initially hidden
”键并将其值设置为“ YES
” 如果没有按键,则可以通过选择顶部的“ information property list
”添加它,然后单击+图标
我用下面的代码解决了这个问题
- (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;
}
#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")];
}
只需在viewWillAppear
设置以下代码即可。
if ([[[UIDevice currentDevice] systemVersion] floatValue]<= 7) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
使用Salesforce SDK 2.1(Cordova 2.3.0),我们必须执行以下操作才能在应用程序的初始加载状态下显示状态栏并从后台(iPhone和iPad)返回:
与此处发布的其他解决方案相反,这个解决方案似乎在设备的旋转中存活。
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- 添加到AppDelegate.m
导入
#import "SFHybridViewController+Amalto.h"
3- 在AppDelegate
的方法didFinishLaunchingWithOptions
的末尾注入
//Make the status bar appear
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
4- 添加到App-Info.plist
属性
View controller-based status bar appearance
,其值为NO
有几种不同的方式。 一种方法是使用.plist文件
这将隐藏整个项目的状态栏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.