self.window not working inside view controller - ios

For iOS 7, I had status bar problem. For that I solved it by using below code in Appdelegate
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[[UIApplication sharedApplication] 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,0, self.window.frame.size.width, self.window.frame.size.height);
}
All is working perfectly.
Only it gives problem while taking photo.
When I open camera or photo gallery, I still see status bar and because of that I see half of the navigation title as shown below.
Any idea how can I overcome this error?
The problems are :
Photo Gallery/ Camera comes like above image
If I click cancel, my view is shifted 20px up automatically.

Instead of:
self.window
Use this:
[[self view] window]
Try Like this:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
self.view.window.clipsToBounds =YES;
self.view.window.frame = CGRectMake(0,20,self.view.window.frame.size.width,self.view.window.frame.size.height-20);
self.view.window.bounds = CGRectMake(0,0, self.view.window.frame.size.width, self.view.window.frame.size.height);
}

I guess you are using the UINavigationController. There is nothing to do with the frame nor to do something with the Status Bar, If you are using UINavigationController. Please prefer the below code to set the UINavigationController along with Status bar set up. Else you can display the code, Will help you fix this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ExViewController alloc] initWithNibName:#"ExViewController" bundle:nil];
self.window.rootViewController = self.viewController;
self.mainViewNavigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = mainViewNavigation;
[self.window addSubview:self.mainViewNavigation.view];
[self.window makeKeyAndVisible];
return YES;
}

I faced this problem wih one of my project. We didn't even have the xib for many classes. So all I could do is reset the frame once again in the delegate functions.
[self.navigationController dismissViewControllerAnimated:YES
completion:^{
[self.navigationController.view setFrame:CGRectMake(self.navigationController.view.frame.origin.x, [UIApplication sharedApplication].statusBarFrame.size.height, self.navigationController.view.frame.size.width, [[UIScreen mainScreen] bounds].size.height-[UIApplication sharedApplication].statusBarFrame.size.height)];
}];
I put this in the finish/cancel delegate methods of photo/camera picket, mail picker.

Related

Application windows are expected to have a root view controller at the end of application launch (Iphone App)

Im trying to make an Iphone app for my coursework and im getting the error
2015-06-26 00:41:31.721 My Movies[3313:6954045] Application windows are expected to have a root view controller at the end of application launch
Im currently running off Xcode 6.3.2 (6D2105)
This is what my AppDelegate.m finish launching options section looks like
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor blackColor];
// Create Film TableView and add it to Navigation Controller, then add Nav'Controller to Window
FilmTableTableViewController *vc = [[FilmTableTableViewController alloc] initWithStyle:UITableViewStylePlain];
navController = [[UINavigationController alloc] initWithRootViewController:vc];
[[self window] addSubview:[navController view]];
[self.window makeKeyAndVisible];
return YES;
Don't add the navcontroller as a subview.
Replace that with
// [[self window] addSubview:[navController view]];
[self.window setRootViewController:navController];

content doesn't rotate if I use UINavigationController

App content doesn't rotate if I use UINavigationController in my app. Only status bar orientation changes.
This is my didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:#"specific"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window setRootViewController:navigationController];
return YES;
}
I dont understand why I am getting this result.
FOUND IT
Finally, I found a solution. But I don't understand why.
this is my didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window.rootViewController =nil;
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:#"specific"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
return YES;
}
And this is viewDidLoad method of SpecificViewController.
- (void)viewDidLoad {
[super viewDidLoad];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
self.navigationItem.title=#"The Title";
}
And I set "View controller-based status bar appearance" property to NO in plist file.
It is working now but I don't understand what do all these mean and why.
I don't think the answer you've accepted is the correct one here, I guess if it works for you that's great but it still looks wrong to me.
The reason rotation wasn't working was because you're using Storyboards which automatically create a UIWindow. Then in your application didFinishLaunching method you are alloc'ing a new UIWindow. Only the first UIWindow (created by your storyboard) will receive the rotation notifications, which is why you only noticed the status bar rotating and not your views.
The solution is to remove the following lines from your didFinishLaunchingWithOptions method:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
Your rootViewController will also be set in the storyboard, there's no need to set it again in code.
This works for me, try:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:#"specific"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
}
add this to SpecificViewController.m
- (BOOL)prefersStatusBarHidden
{
return NO;
}
set "View controller-based status bar appearance" property to YES and remove "Main" from Main Interface options.You don't need to use setStatusBarHidden method.

Issues Starting in Landscape with IIViewDeckController as RootController on iPad

If I set a view deck controller as the AppDelegate window's root controller, when the app starts in landscape orientation on an iPad, the center view is displayed in it's Portrait orientation size and not resized to Landscape.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
IIViewDeckContoller *rootController = [IIViewDeckController new];
self.window.rootViewController = rootController;
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
}
However, if I create a simple controller as a root controller, and then present the view deck controller from this root controller, then everything seems to display just fine.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *simpleRootController = [UIViewController new];
IIViewDeckContoller *deckController = [IIViewDeckController new];
self.window.rootViewController = simpleRootController;
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
// View Deck Controller seems to have issues when it is the root controller of the main window.
// Presenting it as a modal seems to do the trick.
[simpleRootController presentViewController:self.deckController animated:NO completion:nil];
}
Anyone else run into this issue? Is there a better way to solve this? I do not see the same behavior with the iPhone.
I've seen some people having success with the following method in IIViewDeckController:
- (CGRect) referenceBounds {
if (self.referenceView) {
return self.referenceView.bounds;
}
CGRect bounds = [[UIScreen mainScreen] bounds]; // portrait bounds
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
}
return bounds;
}
However, I'm not seeing anything change. I can't take the OP's advice of presenting the view controller due to some other restrictions.

how do I set the orientation of a UIWindow with UINavigation

I'm adding the following code to my application in order to navigate back and forth from a quicklook view.
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self rotateView];
nav = [[UINavigationController alloc] initWithRootViewController:self];
[window addSubview:[nav view]];
[window makeKeyAndVisible];
[[self navigationController] setNavigationBarHidden:YES];
This code is run in my main ViewController viewDidLoad function. My application runs in landscape but this window and navigation bar loads in portrait mode. My application doesn't have a statusBar so some code I've found doesn't work. I get an error if I try to check and listen for status bar orientation notifications.
I'm looking for a way to allow this navigation view to set it's orientation based on the apps current orientation.
If I understand your question Your above code should be placed in didFinishLaunchingWithOptions not in viewDidLoad in the AppDelegate and the initialization of MainViewController should be there also.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.mainViewController = //init your MainViewController;
navigationController = [[UINavigationController alloc] initWithRootViewController:(UIViewController*)viewController];
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}

uinavigationcontroller navigation bar always hidden

I'm developing an iOS 4 application with latest SDK and XCode 4.2.
I'm using an UINavigationController and I don't want to show Navigation Bar. To do that, I use this code on AppDelegate:
- (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];
}
navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBar.hidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
But, this line, navController.navigationBar.hidden = YES; doesn't work. I don't see navigation bar on first view controller, but I see it on others views.
Any clue?
I have achieved this doing the following:
Setting navController.NavigationBar.hidden = YES; in AppDelegate, after alloc it.
Setting [navController setNavigationBarHidden:YES animated:YES]; on viewWillAppear: on every viewController that I pust to navController.
try
[navController setNavigationBarHidden:YES animated:NO]
The doc is here.
I think that the navigationBar has to be set hidden on the view, not on the controller.

Resources