I have returned in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
RootViewController *rvc = [[[RootViewController alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:rvc] autorelease];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
View controller-based status bar appearance is YES in plist file
when i am pushing view controllers status bar is looking great. As showing in following image.
but when i am presenting modelviewcontroller it look as following.
I have written following code in viewDidLoad method of viewcontroller which I am presenting
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
// iOS 7
navBar.frame = CGRectMake(navBar.frame.origin.x, 20, navBar.frame.size.width, 44);
}
I want to display black status bar when presentingmodelview. But it is not displaying.
I tried with
View controller-based status bar appearance is NO also in plist file
but it is not working.
My code is working great in all ios version below 7.0 but not in ios 7.0 and above.
Any solution for this ? Let me know if any one not getting my question.
Good solution found in this question .
1) Set UIViewControllerBasedStatusBarAppearance to NO in info.plist (To opt out of having view controllers adjust the status bar style so that we can set the status bar style by using the UIApplicationstatusBarStyle method.)
2) In AppDelegate's application:didFinishLaunchingWithOptions, call
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.clipsToBounds =YES;
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);
}
return YES;
Related
I have created an application using XIB without Status Bar before for iOS7, now i need to add Status bar on my app and Status bar background color should same as Navigation bar background color. So i have tried like (In my info.plist) :
1) Set View controller-based status bar appearance to NO
2) Set Status bar style to UIStatusBarStyleLightContent
here is my code for App Delegate:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1.0]];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1.0]];
So i am getting the output like the below image :
also i am getting misplacement of UIButtons that i have given below in my screen (It's hiding 20 pixels).
Can you please help me that how can i fix this issue? I need my output like the below image :
Any help will be very appreciated, Thanks.
Third screen :
Add following code in your viewDidLoad method :
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0)
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
Please define this in AppDelegate.h (or) constants.h
#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0)?YES:NO
under viewDidLoad Write this lines.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973 green:0.973 blue:0.973 alpha:1];
//change this to match your navigation bar
[self.window.rootViewController.view addSubview:addStatusBar];
}
OR we can change it manually in the xib.
By moving each element 20px down.
Surprise at how few people have suggested this. Here is the correct way (no hacking)
First, make your navigationBar have a Y origin of 20.
Then in the .h file, set your ViewController as a UIBarPositioningDelegate:
#interface XYZViewController : UIViewController <UIBarPositioningDelegate>
And in the .m file:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBar.delegate = self;
}
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
return UIBarPositionTopAttached;
}
I'm trying to create 2 storyboards, one for iPhone 4 and one for iPhone 5. I want it to be detected at launch which device the user is using. I've used the following code and implemented it in my app delegate.m, but receive the error:
Use of undeclared identifier "initializeStoryBoardBasedOnScreenSize"
Here's the code I've used:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
-(void)initializeStoryBoardBasedOnScreenSize {
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{ // The iOS device = iPhone or iPod Touch
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iOSDeviceScreenSize.height == 480)
{ // iPhone 3GS, 4, and 4S and iPod Touch 3rd and 4th generation: 3.5 inch screen (diagonally measured)
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone35" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
}
if (iOSDeviceScreenSize.height == 568)
{ // iPhone 5 and iPod Touch 5th generation: 4 inch screen (diagonally measured)
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone4" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
}
} else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{ // The iOS device = iPad
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
}
Is there maybe something I need to import to fix the error?
You have tried to define a method inside application:didFinishLaunchingWithOptions::
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-(void)initializeStoryBoardBasedOnScreenSize {
// ... your code ...
}
return YES;
}
This is not what you want and btw. nested functions (or methods) are not supported
in Objective-C.
What you probably meant is to define a method and call it
inside application:didFinishLaunchingWithOptions::
-(void)initializeStoryBoardBasedOnScreenSize {
// ... your code ...
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self initializeStoryBoardBasedOnScreenSize];
return YES;
}
maybe it could just be an error here, but you're entering an action void in didFinishApplicationLaunchingWithOptions?
did you try to fit everything in didFinishApplicationLaunchingWithOptions without simply use this action void?
If my project supports iOS versions 4.3 and up, how can I enable autolayout in iOS6 only, but disable it to the lower versions?
if you are using storyboards, create two storyboards, one with auto-layout on, and other with auto-layout off. Then one way to check is to load either one based on os version.
i used this little macro
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)
this should work too (probably better in some cases then above):
if ([NSLayoutConstraint class]) {
// >= 6.0
} else {
// < 6.0
}
and then in my appDelegate:
UIStoryboard *mainStoryboard = nil;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
{
mainStoryboard = [UIStoryboard storyboardWithName:#"AutoLayoutStoryboard" bundle:nil];
}
else
{
mainStoryboard = [UIStoryboard storyboardWithName:#"NoAutoLayoutStoryboard" bundle:nil];
}
//load initial view controller
UIViewController *rootView = [mainStoryboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = rootView;
[self.window makeKeyAndVisible];
ADD: if you are using nibs, and want to enable auto-layout for iOS6 only, you can create a storyboard (or nibs) with auto-layout on, and load them using above version check
Either you should use two xib files or you should check it programmatically before writing code of auto layouts and constraints.
I am trying to add a UILabel in UIWindow of AppDelegate from a UIViewController. This is how I am doing this:
AppDelegate code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
[self.window makeKeyAndVisible];
self.window.rootViewController = self.viewController;
return YES;
}
ViewController code :
- (void)viewDidLoad
{
UILabel *abcd=[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 40.0)];
abcd.text=#"loading...";
abcd.backgroundColor=[UIColor clearColor];
[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];
[super viewDidLoad];
}
But all I am seeing is grey screen but no label. Where I might be going wrong?
You must not add UILabel to UIWindow, you should add to UIViewController. Change this line:
[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];
for this:
[self.view addSubview:abcd];
1) I suggest you reverse the order of your last two delegate statements:
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
2) While you should be able to add the label to the window, its somewhat unorthodox to do so. In any case, try adding the label to the viewController's view and see if that works, and if so, and you really want to add it to the window (for some reason), then add a comment here:
[self.view addSubview:abcd];
If you still cannot see the label its likely that there is an issue with the view controller. Did you define anything in the nib - any element that should be visible at launch ? If not then add something just so you can be sure the view is in fact getting loaded. [One trick I use is to set the background color of views to red or blue, so I can see that in fact they got loaded.]
revers the order to
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Then
try adding to the view first not to the window using following code
[self.view addSubview:abcd];
If this does not show your label then the view controller is not getting loaded.
If so then check the property of your xib file.
remove the window alloc line and check the hook up for your window in mainwindow.xib file if the hoockup is not correct then it will not load the view.
Maybe your view controller's view is covering the one you added. Add the new view to the view controller's view instead:
[self.view addSubview:abcd];
Try this after adding UILabel in UIWindow
[[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
I have no idea how to set up a menu based project in xcode 4.2.
Basically, the menu has 4 buttons in it each go to their own Navigation Controlled tableviews, but I'm not sure where to start with xcode 4.2.
In my previous application I have a main window that has the navigation controller in that, then there is a root view which sets up the 4 buttons and from there the tableview view controllers just get loaded into the navigation controller.
With xcode 4.2 I cannot seem to set the delegate of the main window, so I cannot figure this out.
So I am hoping someone understands what I am trying to do and can help me out or send me a example or tutorial or something. Thanks, any help would be greatly appreciated.
Here is how I setup my navigation controller and root view in the AppDelegate file. From there you can just add the buttons and so on just as before. If you mean to do it with the storyboard, it will be much easier. Let me know if I misunderstood the question though:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self managedObjectModel];
[self managedObjectContext];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
RootViewController *controller = [[RootViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];
[self.window setRootViewController:navigation];
[controller release];
[navigation release];
[self.window makeKeyAndVisible];
return YES;
}
You'll be fine with one UINavigationController. In your view with the 4 menu buttons just hide the UINavigationController.
Hide / Show UINavigationController