MMDrawerController with UITabBarController showing a black gap on top of the center view - ios

I am opening a UITabBarController with MMDrawerController on the left side.
So from my delegate didFinishLaunchingWithOptions I am making a MMDrawerController with centerNav is a TabBarController and leftNav is a ViewController with Navigation controller. So as expected the tabbar is coming with the mmdrawer but there is a black gap on top of the LandingTabBarController.If I open the LandingTabBarController directly then it is coming from the top. Whats going wrong?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LandingTabBarController *landingTabBarController = [[LandingTabBarController alloc]initWithManagedObjectContext:self.managedObjectContext];
UIViewController * drawerNavController = [storyboard instantiateViewControllerWithIdentifier:#"DrawerViewController"];
UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:landingTabBarController];
UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:drawerNavController];
centerNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
leftNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
_drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
_drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
_drawerController.showsShadow = NO;
//[_drawerController setDrawerVisualStateBlock:[MMDrawerVisualState slideVisualStateBlock]];
[_drawerController setDrawerVisualStateBlock:MMDrawerSideNone];
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
return YES;
}
This is my LandingTabBarController viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
UIViewController *firstViewController = [[UIViewController alloc]init];
[firstViewController.view setBackgroundColor:[UIColor redColor]];
UIViewController *secondViewController = [[UIViewController alloc]init];
[secondViewController.view setBackgroundColor:[UIColor greenColor]];
[tabViewControllers addObject:firstViewController];
[tabViewControllers addObject:secondViewController];
[self setViewControllers:tabViewControllers];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"one" image:[UIImage imageNamed:#"icon_feed"] tag:1];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"two" image:[UIImage imageNamed:#"icon_apps"] tag:2];
}
Screenshot of the debugger

Related

How to set rootViewController for navigation view controller and tabbar controller in AppDelegate

i have this problem. When i set rootViewController for TabbarController, it show correctly. But i set another rootViewController for navigation bar, TabbarController will not able to display. Any idea?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[DCTabBarController alloc] init];
DCTabBarController *tabBar = (DCTabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:2];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:rootNav];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}
After applied Adeel solution, here is the output.But items in tabbar will not display accordingly.
One important thing to mention here is that an application's window can have only one rootViewController (of course). Like I said in my comment as well, you probably want to do something like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
DCTabBarController *tabBar = [[DCTabBarController alloc] init];
[tabBar setViewControllers:#[rootNav]];
self.window.rootViewController = tabBar;
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:tabBar];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}

Navigation Bar not showing title

I am unable to get the navigation bar to show the title that I specify. I have tried to change the title in the AppDelegate.m, as well as in the viewDidLoad of my first tab view. I suspect the title is being hidden, but I am unable fix it. Please provide any insight you may have.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
UIColor *firstColor = [UIColor colorWithRed:155.0f/255.0f green:41.0f/255.0f blue:104.0f/255.0f alpha:1.0f];
UIColor *secondColor = [UIColor colorWithRed:215.0f/255.0f green:90.0f/255.0f blue:18.0f/255.0f alpha:1.0f];
NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];
[[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];
self.navigationController.title = #"This title is not showing";
[[navigationController navigationBar] setTranslucent:NO];
//create the view controller for the first tab
self.firstViewController = [[FirstViewController alloc] initWithNibName:nil
bundle:NULL];
//create the view controller for the second tab
self.secondViewController = [[SecondViewController alloc] initWithNibName:nil
bundle:NULL];
//create the view controller for the third tab
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil
bundle:NULL];
//create the view controller for the fourth tab
self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil
bundle:NULL];
//create an array of all view controllers that will represent the tab at the bottom
NSArray *myViewControllers = [[NSArray alloc] initWithObjects:
self.firstViewController,
self.secondViewController, self.thirdViewController, self.fourthViewController, nil];
//initialize the tab bar controller
self.tabBarController = [[MainUITabBarController alloc] init];
//set the view controllers for the tab bar controller
[self.tabBarController setViewControllers:myViewControllers];
[navigationController setViewControllers:#[self.tabBarController]];
[self.window setRootViewController:navigationController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The relationship of your view controllers isn't right.
The root view controller of your app should be the TabBarController.
Each of the items should have it's own Navigation Controller.
The title of each ViewController can be set in viewDidLoad() with self.title = "....
I've been able to set titles in the delegate as follows:
1.) Create view controllers
2.) Set those titles
3.) Then create UINavigationControllers and assign view controllers there
self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
self.secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil bundle:nil];
self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil bundle:nil];
[self.firstViewController setTitle:#"First"];
[self.secondViewController setTitle:#"Second"];
[self.thirdViewController setTitle:#"Third"];
[self.fourthViewController setTitle:#"Fourth"];
UINavigationController *controller1 = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
UINavigationController *controller2 = [[UINavigationController alloc] initWithRootViewController:self.secondViewController];
UINavigationController *controller3 = [[UINavigationController alloc] initWithRootViewController:self.thirdViewController];
UINavigationController *controller4 = [[UINavigationController alloc] initWithRootViewController:self.fourthViewController];
NSArray *viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, nil];

how to use to uiviewcontroller without xib file

I have one question about transition.
we use this code when we want to transition to next UIViewController:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:secondViewController animated:YES];
or
when we want to add NavigationController using this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
or
when we want add UIViewController to another UIViewController using this code:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.view addSubview:secondViewController.view];
now I want to know that if I don't use xib file in my project what to do???
You could use ViewController from Storyboard
ViewController *controller = (ViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
or create normally
// make Layout in viewController - viewDidLoad
ViewController *viewController = [[ViewController alloc] init];
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 64.0, 320.0, 24.0)];
label.textColor = [UIColor blackColor];
label.text = #"My text";
[self.view addSubview:label];
UIButton *button = ...;
[self.view addSubview:button];
}

Show UITabBar without beeing RootViewController

I'm having an LoginViewController which is the RootViewController. This ViewController should not have the TabBar included. The Rest of the ViewController should contain the UITabBar, But it is not showing. If i make the tabBar the rootController it will show the tabBar in the viewcontrollers. This would also make the firsttab the rootviewcontroller which it should not.
My question is then how can i make the loginview the rootViewController without an tabBar and still show the tabBar in the other viewcontrollers?
My code:
tabBarController = [[UITabBarController alloc] init];
MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];
FixtureViewController *secondTab = [[FixtureViewController alloc] initWithNibName:#"FixtureViewController" bundle:nil];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondTab];
WorldCupViewController *thirdTab = [[WorldCupViewController alloc] initWithNibName:#"WorldCupViewController" bundle:nil];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdTab];
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navController, navController2, navController3];
navController.tabBarItem.image = [UIImage imageNamed:#"message-7"];
navController2.tabBarItem.image = [UIImage imageNamed:#"football-32"];
navController3.tabBarItem.image = [UIImage imageNamed:#"trophy-32"];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:110/255.0f green:89/255.0f blue:196/255.0f alpha:1.0f]];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
What you need to do is transition the application from the LoginViewController to the Tabbar. What I would suggest doing is replacing the LoginViewController with the TabBar as the rootViewController.
Here is some sample code, perform this action in your AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
return YES;
}
-(void)transitionToTabBar
{
// Set the TabBar how you are in your sample code, this is just an example.
[self.window setRootViewController:[[UITabBarController alloc] initWithNibName:#"SomeNib" bundle:Nil]];
[UIView transitionWithView:self.window duration:0.5f options:UIViewAnimationOptionTransitionCurlDown animations:^{
[self.window makeKeyAndVisible];
} completion:nil];
}
You should use main View controller (for MenuViewController) and by using segue you can present another view controller which has TabBar in it.

PushView on PresentViewController iOS

I want to Push my viewController on PresentViewController
When i click on button initially i am loading PresentViewController, here is my code.
- (IBAction)JoinClicked:(id)sender{
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.view.window.rootViewController presentViewController:detail_view_controller
animated:YES
completion:NULL];
}
this works fine, but when i click on button which is there on PresentViewController on click i want to Push my view, but in does not pushes.
Please help, Thanks in advance.
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.navigationController pushViewController:detail_view_controller animated:YES];
Try like this to push viewController. If you use TabBar do like this in AppDelegate. If you create TabBar Drag and drop means leave that. Create TabBar Programatically like below.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabController = [[UITabBarController alloc]init];
self.viewController = [[CalculatorViewController alloc] initWithNibName:#"CalculatorViewController" bundle:nil];
UITabBarItem *tab_item1 = [[UITabBarItem alloc]init];
//tab_item1.title = #"Calculator";
tab_item1.image = [UIImage imageNamed:#"Calculator.png"];
[self.viewController setTabBarItem:tab_item1];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:self.viewController];
ShowPhoneViewController *phone = [[ShowPhoneViewController alloc]init];
UITabBarItem *tab_item2 = [[UITabBarItem alloc]init];
tab_item2.title = #"Latest Mobiles";
tab_item2.image = [UIImage imageNamed:#"Mobile.png"];
[phone setTabBarItem:tab_item2];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:phone];
CurrencyConvertorViewController *currency = [[CurrencyConvertorViewController alloc]init];
UITabBarItem *tab_item3 = [[UITabBarItem alloc]init];
tab_item3.title = #"Units Convertor";
tab_item3.image = [UIImage imageNamed:#"Dollar.png"];
[currency setTabBarItem:tab_item3];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:currency];
SettingsPageViewController *setting = [[SettingsPageViewController alloc]init];
UITabBarItem *tab_item4 = [[UITabBarItem alloc]init];
tab_item4.title = #"Settings";
tab_item4.image = [UIImage imageNamed:#"Settings.png"];
[setting setTabBarItem:tab_item4];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:setting];
tabController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES;
}
I hope you got now
pushviewcontroller is the feature of UINavigationController where you can push one viewcontroller on another vierw controller. Here you are using a single viewcontroller as a rootviewcontroller so either you must change your rootviewcontroller to UINavigationcontroller or you can use "addSubview method" to add new viewController on the current viewcontroller.
but the better option is to add uinavigationcontroller as a rootviewcontroller.
AppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *first = [[FirstViewController alloc]
initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navController =[[UINavigationController alloc]
initWithRootViewController:first];
[self.window setRootViewController:navController];
}
Now when as you want to switch from FirstViewController to SecondViewController on button clicked than you have to use pushviewcontroller
FirstViewController.h
-(void) nextBtnPressed {
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:second animated:TRUE];
}

Resources