ViewController loaded in AppDelegate BUT shifted down - ios

Inside AppDelegate.m file I have:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.MainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.HamMenuVC = [[HamMenuViewController alloc] initWithNibName:#"HamMenuViewController" bundle:nil];
self.RevealVC = [[SWRevealViewController alloc]initWithRearViewController:self.HamMenuVC frontViewController:self.MainVC];
self.navC = [[UINavigationController alloc]initWithRootViewController:self.RevealVC];
self.window.rootViewController = self.navC;
[self.window makeKeyAndVisible];
Which is supposed to load my Main VC and then the hamburger menu VC (or slider? in iOS talk), and when I start the app I get the loaded VCs BUT the entire view seems to be shifted down roughly 30 pixels and there's this white space above.
As you can see below, this is the top of my screen and the black is where the MainVC starts:
the top of the screen
I've checked the 'self.view.frame.origin.y' inside the MainVC and it says 0.0 AND I've checked the '[[UIScreen mainScreen] bounds].size.height' and it's giving me the correct height for the device I'm running it on AND I checked the '[[UIScreen mainScreen] bounds].origin.y' and it says 0.0.
Does anyone have a clue why all the data is saying it's in the right place but the fact is it's in the wrong place on my physical device AND on any simulator I choose?

It looks like that's the UINavigationBar on top of the screen. In your code, you're setting your rootViewController with a UINavigationController.
self.navC = [[UINavigationController alloc]initWithRootViewController:self.RevealVC];
self.window.rootViewController = self.navC;
If you need to hide the navigation bar, call this method in your RevealVC's viewDidLoad method:
[self.navigationController setNavigationBarHidden:YES animated:YES];
Also, quick tip, try to name your properties starting with lowercase letters.

Related

iOS navigation controller without navigation bar

This is how I launched by main controller in my iOS app.
UIViewController *rootController = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
With this I am getting a blank bar at the top.
I want to use a navigation controller to control the flow of screens that users are able to reach by pressing back but I don't want a navigation bar. How can I change my code to get what I want?
You can use this code right after you declare navigationController = ... in the code sample you provided:
[navigationController setNavigationBarHidden:true];
And then whenever you need to go "back," your app would run this code from within the current view controller:
[self.navigationController popViewControllerAnimated:true];

navigation controller navigation bar only covers top left half of screen in plain vanilla code

I'm just doing some ios programming after not touching it for a few months, and I'm new to it this year. I just started a new project, and all I did was programmatically set up a navigation controller as the root view controller.
Here's the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UINavigationController *nvc = [[UINavigationController alloc] init];
nvc.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
nvc.view.backgroundColor = [UIColor yellowColor];
nvc.title = #"Woah";
// nvc.navigationBarHidden = TRUE; this works ok
self.window.rootViewController = nvc;
return YES;
}
I don't have the reputation points to post an image, but here it is: http://i.stack.imgur.com/tbcqi.png
As you can see the title does not show in the navigation bar. Also, quite oddly, the navigation bar is only across the top left corner and not across the entire view. What am I missing here? Also if I set the commented out line to run, to hide the bar, that does work. But of course I want the bar, I don't not want it.
This is a brand new project I just started fresh, and I tried it in two new projects. I have the same outcome in both cases of this weird left corner bar. Any ideas what I am doing wrong? Thanks.
You need to add a UIViewController as the root of you UINavigationController. At the moment you have no view controllers being managed.
The navigation bar title reflects the contents of each UIViewControllers UINavigationItem. By default the title on the navigation bar is the title of the UIViewController which is top most in the navigation stack.
The back button oddly reflects the title of the UINavigationItem of the UIViewController second from top. So to set the back button label you actually set it in the navigationItem of the controller which pushed the top controller on.
You can change the title by accessing the top view controllers navigationItem or from inside the top view controller using:
<a view controller>.navigationItem.title = #"My New Title";
or if from within the controller:
self.navigationItem.title = #"My New Title";
I am not sure what is your intention to create UIWindow because it's the code for ages ago.
Now when you are upgrade your xCode to the latest version, there's no need to UIWindow to be created manually.
Also, when you are creating UINavigationController, it is much better when you are setting UINavigationController's rootView controller.
I am quite sure there will be UIViewController which need to be nested in UINavigationController.
So your code will need to be changed like below;
HomeViewController *homeVC = [[HomeViewController alloc] initWithNibName:#"HomeViewController"]; //Let's assume this is the first VC on the navigation stack
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:homeVC];
// nvc.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// nvc.view.backgroundColor = [UIColor yellowColor]; use nvc.navigationBar.tintColor = [UIColor yellowColor];
// nvc.title = #"Woah"; use homeVC.title = #"Woah";
// nvc.navigationBarHidden = TRUE; this works ok
self.window.rootViewController = nvc;

IOS 7 pushviewcontroller error

This snippet of IOS code used to work prior to IOS 7 to push a new window. It does not anymore
ViewController *secondView = [[ViewController alloc]
initWithNibName:#"ViewController"bundle:nil];
[[self navigationController] pushViewController:secondView animated:YES];
Why doesn't this work anymore?
-------EDIT-----
This is the nav controller in didFinishLaunchingwWthOption
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.launchViewController = [[LaunchViewController alloc]
initWithNibName:#"LaunchViewController" bundle:nil];
self.window.rootViewController = self.LaunchViewController;
[self.window makeKeyAndVisible];
return YES;
-------EDIT----------
The first time I invoke [self addNewView] the view does not appear, with no errors in the console or crashes, I added a button and if on the button press I invoke [self addNewView] the view appears, if I navigate with the back button, I have to tap back twice to return to the initial view. So the view is being created but not shown. Any hints on what could be the problem?
Have you tried to specify tha bundle?
ViewController *secondView = [[ViewController alloc]
initWithNibName:#"ViewController" bundle: [NSBundle mainBundle]];
check this:
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:self.launchViewController];
self.window.rootViewContro‌​ller = navi;
There is mo navigationController.window.rootViewController should be a navigationController as your logic.

Objective-C: how to set keywindow to different viewcontroller in Xcode?

I am using storyboards and this code but nothing is appearing in my current viewConntroller but is in my first viewConntroller. Can anybody eli me set the keywindow to the current view i am on?
Here is the code i am using to show my UINavigationBar
[[[UIApplication sharedApplication] keyWindow] addSubview:bar];
Thanks in advance
you dont set the Viewcontroller to a Window
you need to do something like this:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
this is how you add a uinavigationcontroller:
Add a UINavigationBar to a UITableViewController without a UINavigationController
if you want to present a new view controller
you simply use presentModalViewcontroller:
UIViewController *vc = [[UIViewController alloc] init];
[self.navigationcontroller vc animated:YES];
// I have written it all down just check the case and use it

setRootViewController: doesn't work as expected after setViewControllers: have been called

I created a view controller programmatically and set it as a root controller. All worked perfect as it was expected:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
[[self window] setRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
Then I added an UITabBarViewController, set its 'viewControllers' property to point to (an array to) the main viewController 'vc'.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
NSArray *controllers = #[vc];
[tbc setViewControllers:controllers];
[[self window] setRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
and the view stoped to show at the screen upon launch, also there is warning about the root view controller wasn't set. After adding the string below the view of 'vc' controller is finally loaded, but there is a blank line at the bottom of the screen, as if the UIBarController modified [[UIMainScreen bounds].
[[self window] addSubview:vc.view];
I'm new to iOS development, and I'm a bit confused. It seems I don't understand some very fundamental things about the view controllers hierarchy, but after reading the "View Controller Programming Guide" by Apple, I still don't understand where am I wrong.
The setRootViewController: method should auto assign the _view of argument view controller as default view of the window, but it doesn't happen if the named view controller was already previously pointed by viewControllers property of UITabBarViewController. Though I checked the debugger and found that 'vc' object isn't changed after setViewControllers: method is called.
Could you please explain me what is going on or point me to a documentation I should read?
UPDATE: I'm not going to insert the 'vc' controller into the 'tbc' controller. What I'd like is to display the 'vc' view fullscreen, as it would normally displayed without the code about 'tbc'.
From my point of view, adding another view (tbc in my case) should NOT affect this behaviour.
Of course, that's pretty useless from practical point of view, but I'd like to know what's going on under the hood.
You should set tbc as rootViewController.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
NSArray *controllers = #[vc];
[tbc setViewControllers:controllers];
[[self window] setRootViewController:tbc];
[[self window] addSubview:tbc.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
When you add vc to the tab bar controller, it becomes a child of that controller. The root view controller of the window can't be a child, which is why you get that error. If you want vc to be full screen, then don't put it in the tab bar controller, and then at some point, you switch the window's root view controller to be the tab bar controller (if that's what you want).
You haven't said what you want to use vc for. A better way, depending on its use, might be to present it modally (so it takes the whole screen) from whichever controller is in the first tab of your tab bar controller. Do this from viewDidAppear, and it will be the firsts thing the user sees when the app starts up.

Resources