I am creating a UINavigationBar using the following,
UINavigationController *navigationController = [[UINavigationController alloc] init];
[view addSubview: navigationController.view];
How come it shows up like this?
(source: gyazo.com)
I want it to be on the top of the view but there is space there, why?
Thanks.
You are not creating things correctly. You should create your view controller, then create a navigation controller, passing the view controller as the nav controller's root controller. Then you make the nav controller the app's root controller.
UIViewController *vc = ... // create your view controller
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
window.rootViewController = nc;
The code you posted does not create a nav bar, it creates an entire navigation controller.
Put this code to ViewWillAppear
- (void)viewWillAppear {
UINavigationController *navigationController = [[UINavigationController alloc] init];
[view addSubview: navigationController.view];
}
Also you should resize nav controller to self controller bouns
Related
I have a UITabBarController which contains four navigation controllers.
When user taps a button , I want to set the root viewController of window to a new navigation controller.
How do I do that in Objective-C?
Using a modal view controller will look better than simply replacing the root view controller, and should produce the same effect as you are looking for. Basically, you create an instance of your view controller, add it to a new navigation controller, and then present the navigation controller modally:
- (void)buttonPressed
{
MyViewController *vc = [[MyViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];
}
If I present a controller with a view controller, is it part of the self.navigationcontroller stack?
In essence:
UBSLoginViewController* loginView = [[UBSLoginViewController alloc] initWithNibName:LOGINVIEW bundle:nil];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:loginView];
navigation.navigationBarHidden = YES;
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
[loginView presentViewController:[[UBSLoadingViewController alloc] initWithNibName:LOADINGVIEW bundle:nil] animated:YES completion:nil];
Is the loading view part of the navigation stack? Essentially, I want to present a modal view that will not be part of the root navigation stack.
If I present a controller with a view controller, is it part of the
self.navigationcontroller stack?
No.
You need to push the view controller onto your UINavigationController in order for it to be on the navigation controller's stack. Right now you are modally presenting on a view controller which is an entirely different concept.
Pushing onto a navigation controller looks something like this.
[self.navigationController pushViewController:loadingViewController animated:YES];
I have a navigation controller which has view controller(VC1) . This view controller has 3 button out of which 1 directs to a tab bar controller. From tab1 of the tab bar controller a button is there which navigates to a View controller(vc2).
The problem is VC2 is not displaying the tab bar. How can I make my VC2 display the tab bar.
Navigation Controller—> View Controller-->Tab bar Controller —>Tab1 -> View Controller (does not show tab bar)
I am doing this in IOS
Using a tabbarcontroller within a navigation controller is not recommended by Apple. However it is possible to do so.
In the VC1, write the following code.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
MyStartViewController *startController = [[MyStartViewController alloc] initWithNibName:#"MyStartViewController" bundle:Nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:startController];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.viewControllers = #[navController, viewController2];
[self.navigationController pushViewController:tabBarController animated:YES];
Now within startController, add a UIButton. And in the button action, push the new VC2 from it.
Button action:
- (IBAction)buttonPressed {
MyViewController2 *vc2 = [[MyViewController2 alloc] initWithNibName:#"MyViewController2" bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
}
Hope this will serve your purpose.
I had an application in which I need to have a login or register system before the tab bar controller is added to the window, such as in Instagram.
I have added 5 navigation controllers (with view controller as its root) to the UITabBarController and then set it as the root of the window. Before that, I need to have another UINavigationController for the login system.
If I add that, how do I remove it before adding tab?
Another problem is that I also have to handle logging out, so I need to come back to it.
Can anybody help me with me how to do this?
I suggest to you loading Loginview From Delegate window as we did Normally. and from Logged Success its button click you set TabbarController like this:-
UIViewController *viewControllerPostalCode2 = [[cntrServices alloc] initWithNibName:#"cntrServices" bundle:nil];
UIViewController *viewControllerPostalCode3 = [[cntrInquiryViewController alloc] initWithNibName:#"cntrInquiryViewController" bundle:nil];
UINavigationController *navPostage1 = [[UINavigationController alloc] initWithRootViewController:viewControllerPostalCode2];
UINavigationController *navPostage2 = [[UINavigationController alloc] initWithRootViewController:viewControllerPostalCode3];
//
navPostage1.navigationBar.tintColor =DARK_BACKGROUNDNavigation;
navPostage2.navigationBar.tintColor =DARK_BACKGROUNDNavigation;
//
self.tabBarForServicesController = [[UITabBarController alloc] init];
self.tabBarForServicesController.delegate=self;
self.tabBarForServicesController.viewControllers = [NSArray arrayWithObjects:navPostage1,navPostage2,nil];
[self.navigationController pushViewController:self.tabBarForServicesController animated:YES];
I Done this type of Task using this method and at the Logged Out just poptoRootviewController work back to the Logged in Screen.
I have done this like adding my tabbarcontroller as rootViewController to UIWindow and in applicationbecomeactive delegate i present a controller with navigation controller like when required and simply dismiss the controller when authentication done
UIViewController *topViewController = [self.navController topViewController];
if (![topViewController isKindOfClass:[LGLoginViewController class]]) {
[self.navController popToRootViewControllerAnimated:YES];
self.navController = nil;
LGLoginViewController* loginView = [[LGLoginViewController alloc] initWithNibName:#"LGLoginViewController"bundle:nil];
if (!self.navController) {
self.navController = [[UINavigationController alloc] initWithRootViewController:loginView];
} else {
[self.navController initWithRootViewController:loginView];
}
self.navController.delegate = self;
[self.window.rootViewController presentModalViewController:self.navController animated:NO];
}
I have a problem trying to access a navigation controller of the view controller from it, always returns as nill to me, though it is shown within the navigation controller.
Here is what I have (I have a split view controller, that is presented as tab controller for master and viewcontroller (inside navigation controller) as detail):
FirstDetailViewController *fdvc = [[FirstDetailViewController alloc] initWithNibName:#"FirstDetailViewController" bundle:nil];
UINavigationController *fdvcNav = [[UINavigationController alloc] initWithRootViewController:fdvc];
NSArray *ipadVCs = [[NSArray alloc] initWithObjects:tabController, fdvcNav, nil];
UISplitViewController *splitvc = [[UISplitViewController alloc] initWithNibName:nil bundle:nil];
[splitvc setViewControllers:ipadVCs];
[[splitvc view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"splitViewControllerBG"]]];
[splitvc setDelegate:fdvc];
[[self window] setRootViewController:splitvc];
[[self window] makeKeyAndVisible];
But when I'm trying to access a navigation controller from the fdvc view controller in ViewDidLoad with [self navigationController] it gives me (Null) always.
Thanks!
I fixed it. Turned out, that I had to move my code from ViewDidLoad method to ViewDidAppear and it worked fine.
viewDidLoad is getting called before the navigationController property has been updated, that was my mistake.