I am migrating my application to iOS 7. For handing the status bar issue I have added this code
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
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:
Fix for status bar issue in 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.
Steps to do that.
Xcode 5 provide preview option to view the appearance of the xib in different view based on the OS version.
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.
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.
Step 3 : Set the delta value of origin y to -20 for all the controls then 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.
Once the status bar issue fixed, issue while presenting the model view (ZbarSDk controller) is also fixed automatically.
Preview screen :
I am late for this Answer, but i just want to share what i did, which is basically
the easiest solution
First of all-> Go to your info.plist File and add Status Bar Style->Transparent Black Style(Alpha of 0.5)
Now ,here it Goes:-
Add this code in your 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;
}
set the following to info.plist
View controller-based status bar appearance = NO;
To hide status bar in ios7 follow these simple steps :
In Xcode goto "Resources" folder and open "(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"
If the keys are not there then you can add it by selecting "information property list" at top and click + icon
MUCH MUCH MUCH simpler answer:
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 answer for 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 particular example, 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...
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
#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
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):
Contrarily to other solutions posted here, this one seems to survive rotation of the device.
1-Create a category of theSFHybridViewController
#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
#import "SFHybridViewController+Amalto.h"
3-Inject at the end of of method didFinishLaunchingWithOptions of 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
View controller-based status bar appearance with value NO
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;
}
#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")];
}
There are several different ways. One approach is to use .plist file
Add a new key "View controller-based status bar appearance" and set value as "NO".
Add another key "Status bar is initially hidden" and set value as "YES".
This will hide status bar throughout project.
just set the following code in viewWillAppear.
if ([[[UIDevice currentDevice] systemVersion] floatValue]<= 7) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Related
In order to fix the overlapping of status bar and navigation bar in iOS 7+, i'm using this code inside didFinishLaunchingWithOptions in AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//some codes
//.
//.
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
UIView *FakeNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
FakeNavBar.backgroundColor = UIColorFromRGB(0x55BCAFF);
float navBarHeight = 20.0;
for (UIView *subView in self.window.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height - navBarHeight);
} else {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height);
}
}
[self.window addSubview:FakeNavBar];
}
}
It pushes all my controllers and views 20 pixels down and and the overlapping problem gets fixed but when i reach my tab view controller scene, then the tab bar on the bottom goes out of view by 20 pixels.
So how can i keep the tab bar in its place while shifting everything else down?
It would also work if i could just shift up only the tab bar by 20 pixels.
I was able to shift only the tab bar 20 pixels up but this may put some views behind the tab bar which is unwanted.
here is the code written inside viewDidAppear of my UITabBarController class :
-(void)viewDidAppear:(BOOL)animated{
CGRect newFrame = self.tabBar.frame;
newFrame.origin.y -= 20;
self.tabBar.frame = newFrame;
}
Please use the code below -
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
UIView *FakeNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
FakeNavBar.backgroundColor = UIColorFromRGB(0x55BCAFF);
float navBarHeight = 20.0;
for (UIView *subView in self.window.subviews) {
for (int index=0; index<[tabBarController.viewControllers count]; index++)
{
if ([subView isKindOfClass:[[(UIViewController*)[tabBarController.viewControllers objectAtIndex:index] view] class]])
{
continue;
}
}
if ([subView isKindOfClass:[UIScrollView class]]) {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height - navBarHeight);
} else {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height);
}
}
[self.window addSubview:FakeNavBar];
}
I assume that you're not using a navigation controller and you've manually added the navigation bar in your view. Is that right?
You should be able to achieve what you're after by adding a view with a size of the status bar at the top of the views of your view controllers in the storyboard.
Are you using auto-layout constraints? You could use them to make these views stick to the top of your views, have a fixed height of 20 pixels and a width equal to the width of the view controller's view.
So I roughly followed this tutorial on how to make an iAd banner not cover a Phonegap app, but had to improvise because it didn't really work. So in my webViewDidFinishLoad in my mainViewController method, here is what I have:
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
adView.frame = CGRectOffset(adView.frame, 0, [[UIScreen mainScreen] bounds].size.height - 70);
adView.delegate = self;
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
[theWebView addSubview:adView];
[self.view bringSubviewToFront:adView];
return [ super webViewDidFinishLoad:theWebView ];
}
adView has been properly initialized and is functioning properly. What breaks this (as in I can't click the banner) is this code in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 70;
self.webView.frame = viewBounds;
}
[super viewWillAppear:animated];
}
I added the 70px offset in order to have the banner not cover the content. Now, if I remove this code, I can click the banner fine. What is wrong?
Silly me. I was adding the subview to theWebView instead of self.view, which made it outside of its boundary and unclickable.
I am using ios 7 I want to set stauts bar background image.
I have done this but still it is not changing anything:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor redColor]; //change this to match your navigation bar
[self.window.rootViewController.view addSubview:addStatusBar];
}
I have done this like .h file
#property (retain, nonatomic) UIWindow *statusBarBackground;
and in .m file
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.statusBarBackground = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
self.statusBarBackground.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:#"statusbar_bg"]];
[self.statusBarBackground makeKeyAndVisible];
}
add this to your controllers
- (void) viewDidLayoutSubviews {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
CGRect viewBounds = self.view.bounds;
if (viewBounds.origin.y == 0) {
CGFloat topBarOffset = self.topLayoutGuide.length;
viewBounds.origin.y -= topBarOffset;
self.view.bounds = viewBounds;
}
}
}
your code works, but you have to modify it a bit. here is what it should look like..
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
//change this to match your navigation bar or view color or tool bar
//You can also use addStatusBar.backgroundColor = [UIColor BlueColor]; or any other color
addStatusBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg1.png"]];
//here you are adding the image as the background image
[self.window.rootViewController.view addSubview:addStatusBar];
don't forget to import your image to the project. now i have just plugged in the above code in application didfinishwithoptions of the app delegate, but you should be able to use the same if you want different views using the same.
You have to do 2 things.
(1) Open your info.plist and set "View controller-based status bar appearance" = NO
(2) add these lines to application:didFinishLaunchingWithOptions
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.window.clipsToBounds = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
self.window.frame = CGRectMake(20, 0,self.window.frame.size.width-20,self.window.frame.size.height);
self.window.bounds = CGRectMake(20, 0, self.window.frame.size.width, self.window.frame.size.height);
} else
{
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
}
My app has a tableview controller that is presented as a form sheet.
On top portion of tableviewcontoller there is a UView and inside of that UIView there is a navigation bar and a searchbar.
Everything works fine older version of IOS but in IOS7 when user taps searchbar everything is messes up.
Normal:
When User starts to type:
After seach ends:
in.h
UITableViewController<UITextFieldDelegate,UISearchDisplayDelegate,UISearchBarDelegate>
#property (nonatomic,weak) IBOutlet UINavigationBar *topBar;
Tried few things but code doesnt seem to be changing anything, when put a breakpoint it enters to delegate methods though
in.m
//-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
// if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
// CGRect statusBarFrame = self.topBar.frame;
// [UIView animateWithDuration:0.25 animations:^{
// for (UIView *subview in self.tableView.subviews)
// subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height+50);
// }];
// }
//}
//
//-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
// if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
// [UIView animateWithDuration:0.25 animations:^{
// for (UIView *subview in self.tableView.subviews)
// subview.transform = CGAffineTransformIdentity;
// }];
// }
//}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect frame = self.searchDisplayController.searchBar.frame;
frame.origin.y += self.topBar.frame.size.height;
self.searchDisplayController.searchBar.frame = frame;
}
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect statusBarFrame = self.topBar.frame;
CGRect frame = self.searchDisplayController.searchBar.frame;
frame.origin.y -= statusBarFrame.size.height;
self.searchDisplayController.searchBar.frame= frame;
}
}
#Additional Info
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
//self.searchDisplayController.searchBar.searchBarStyle= UISearchBarStyleProminent;
self.edgesForExtendedLayout = UIRectEdgeNone;
//self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight;
}
}
According to break points frame position and sizes are correct but they dont change self.searchDisplayController.searchBar.frame at all
I have also tried
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[self.topBar setHidden:YES];
}
}
-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[self.searchDisplayController.searchBar removeFromSuperview];
CGRect frame = self.searchDisplayController.searchBar.frame;
frame.origin.y += self.topBar.frame.size.height;
self.searchDisplayController.searchBar.frame = frame;
[self.topView addSubview:self.searchDisplayController.searchBar];
[self.topView bringSubviewToFront:self.topBar];
[self.topBar setHidden:NO];
}
}
How can I solve this issue ?
try setting this value in your table view controller:
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]){
self.edgesForExtendedLayout = UIRectEdgeNone;
}
And change you view hierarchy to have a view, and tableView, search bar and nag bar as it's subviews. Make your table view controller a view controller and make it as the data source and delegate.
Or don't use a searchBarDisplayController and just use a search bar with it's delegate methods:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
I had a similar problem, I think searchDisplayController expands searchBar size to full tableHeaderView size (I really don't know why it doing this). I had searchbar and one custom toolbar (both 44px height) in tableHeaderView.
I've workarounded it with:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
self.tableView.tableHeaderView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44);
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
self.tableView.tableHeaderView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 88);
}
So I just setting tableHeaderView to the size of single UISearchBar when entering search and set it back when all animations complete. This solved my problem. Even animations still work (but they does not in the childViewController. No idea why)
I have an app with a support landscape and portrait mode. And I need the same behavior status bar like on iOS 6. What is the simplest way to do this?
I've tried the solutions in Stack Overflow question iOS 7 status bar back to iOS 6 style?, but it doesn't work. My subview depend on the view size, and my view doesn't stretch correctly. I don't want to update my XIB files; I simply want to add something that helps me. I don't know what it may be (hack or prayers).
You can try writing this in your ViewWillappear or DidAppear. Here we are shifting the view frame 20 pixels down.
CGRect frame = self.view.frame;
frame.origin.y = 20;
if (self.view.frame.size.height == 1024 ||
self.view.frame.size.height == 768)
{
frame.size.height -= 20;
}
self.view.frame = frame;
This will work, but however this is not a very good idea. You can also change the text colour of the status bar to light or dark depending on your app background by calling the following method if it helps.
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent; // For light status bar
return UIStatusBarStyleDefault // For Dark status bar
}
If you are on Xcode 5 and you are installing in iOS 7 then sorry, this will not happen (as far as I know).
If you want to see the status bar on iOS 7 like iOS 6 than open your project in Xcode 4.x.x and install in iOS 7. One problem with this approach I found is that sometimes Xcode 4.x.x doesn't recognise an iOS 7 device.
But if your Xcode 4.x.x can show your iOS 7 device then it will work.
The .api generated from Xcode 4.x.x will work in both iOS 6 and iOS 7, but you will not get extra space (of the status bar) on iOS 7 and the new look of keyboard, picker, switch, etc. But yes, you will get the new UIAlertView (I don't know why this is new and the other controls are old.)
I hope we will soon get a better solution in Xcode 5 for this.
UPDATE:
I found the way to run the app from Xcode 5 as Xcode 4. This is just matter of the base SDK.
If you want to built as Xcode 4 (iOS 6 SDK) from Xcode 5 then do the following.
Close Xcode 4 and 5.
In Xcode 4 Go to
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs
Here you will find iPhoneOS6.1.sdk. Copy this folder. And now go in Xcode 5 on the same path. In Xcode 5, you will find iPhoneOS7.0.sdk. Paste iPhoneOS6.1.sdk with it.
Now close the Finder and launch Xcode 5. Go to project target setting -> Build Setting and find Base SDK. Select iOS 6.1 as Base SDK. This will also work for 6.0. You just need to find iPhoneOS6.0.sdk.
Now you will see the device name twice in the run dropdown box. One for SDK 7.0 and one for SDK 6.1. So now you can run both ways with iOS 6 SDK and iOS 7 SDK.
I hope this will help someone.
I recently had to solve a similar problem, and I approached it in a slightly different way...
The approach was to use an extra view controller that acted as a container view controller for what was originally my rootViewController. First, i set up a container like this:
_containerView = [[UIView alloc] initWithFrame:[self containerFrame]];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_containerView.clipsToBounds = YES;
[self.view addSubview:_containerView];
[self.view setBackgroundColor:[UIColor blackColor]];
[UIApplication.sharedApplication setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
where the containerFrame was defined like this:
- (CGRect)containerFrame
{
if ([MyUtilityClass isSevenOrHigher])
{
CGFloat statusBarHeight = [MyUtility statusBarHeight]; //20.0f
return CGRectMake(0, statusBarHeight, self.view.bounds.size.width, self.view.bounds.size.height - statusBarHeight);
}
return self.view.bounds;
}
Finally, I added what was originally my rootViewController as a childViewController of the new one:
//Add the ChildViewController
self.childController.view.frame = self.containerView.bounds;
self.childController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self addChildViewController:self.childController];
[self.containerView addSubview:self.childController.view];
[self.childController didMoveToParentViewController:self];
Things to note:
- Modal view controllers will still be presented in the iOS7 style, so I still have to account for that somehow.
Hope this helps someone!
This Guide helps me.
http://www.doubleencore.com/2013/09/developers-guide-to-the-ios-7-status-bar/
The most robust way to handle the 20 point size difference is Auto Layout.
If you aren’t using Auto Layout, Interface Builder provides you with tools to handle the screen size difference between iOS 7 and the older versions. When Auto Layout is turned off, you will notice an area in the sizing tab of the utility area (right pane) of Interface Builder that allows you to set iOS 6/7 Deltas.
1) It's a hack, but it works!
Use it if you doesn't use UIAlertView or KGStatusBar!!!
#import <objc/runtime.h>
#interface UIScreen (I_love_ios_7)
- (CGRect)bounds2;
- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation;
#end
#implementation UIScreen (I_love_ios_7)
- (CGRect)bounds2
{
return [self boundsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation
{
CGRect resultFrame = [self bounds2];
if(UIInterfaceOrientationIsLandscape(orientation))
resultFrame.size.width -= 20;
else
resultFrame.size.height -= 20;
return resultFrame;
}
#end
void Swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
Swizzle([UIScreen class], #selector(bounds2), #selector(bounds));
[application setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.clipsToBounds =YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidChangeStatusBarOrientation:)
name:UIApplicationWillChangeStatusBarOrientationNotification
object:nil];
NSDictionary* userInfo = #{UIApplicationStatusBarOrientationUserInfoKey : #([[UIApplication sharedApplication] statusBarOrientation])};
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillChangeStatusBarOrientationNotification
object:nil
userInfo:userInfo];
}
return YES;
}
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
CGSize size = [[UIScreen mainScreen] boundsForOrientation:orientation].size;
int w = size.width;
int h = size.height;
float statusHeight = 20.0;
switch(orientation){
case UIInterfaceOrientationPortrait:
self.window.frame = CGRectMake(0,statusHeight,w,h);
break;
case UIInterfaceOrientationPortraitUpsideDown:
self.window.frame = CGRectMake(0,0,w,h);
break;
case UIInterfaceOrientationLandscapeLeft:
self.window.frame = CGRectMake(statusHeight,0,w,h);
break;
case UIInterfaceOrientationLandscapeRight:
self.window.frame = CGRectMake(0,0,w,h);
break;
}
}
#end
2) Create category, and always use contentView instead of view
#interface UIViewController(iOS7_Fix)
#property (nonatomic, readonly) UIView* contentView;
- (void)updateViewIfIOS_7;
#end
#implementation UIViewController(iOS7_Fix)
static char defaultHashKey;
- (UIView *)contentView
{
return objc_getAssociatedObject(self, &defaultHashKey)?: self.view;
}
- (void)setContentView:(UIView *)val
{
objc_setAssociatedObject(self, &defaultHashKey, val, OBJC_ASSOCIATION_RETAIN_NONATOMIC) ;
}
- (void)updateViewIfIOS_7
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7 || objc_getAssociatedObject(self, &defaultHashKey))
return;
UIView* exchangeView = [[UIView alloc] initWithFrame:self.view.bounds];
exchangeView.autoresizingMask = self.view.autoresizingMask;
exchangeView.backgroundColor = [UIColor blackColor];
UIView* view = self.view;
if(self.view.superview){
[view.superview addSubview:exchangeView];
[view removeFromSuperview];
}
[exchangeView addSubview:view];
self.view = exchangeView;
CGRect frame = self.view.bounds;
frame.origin.y += 20.0;
frame.size.height -= 20.0;
view.frame = frame;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self setContentView:view];
}
In every UIViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateViewIfIOS_7];
UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
lab.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:lab];
//...
}