So my app has a UITabBarController. Within that, one of the tabs is a UIViewController containing a UINavigationController. Within that is a UITableViewController.
The problem is that for some reason the size of the TableView is off, and part of the bottom entry of the table is obscured by the TabBar. I've tried everything I can think of to resize the tableview but to no avail. Here are the relevant parts of the code:
AppDelegate
tabBarController = [[UITabBarController alloc] init];
LogbookViewController *firstVC = [[LogbookViewController alloc] init];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: firstVC, secondVC, thirdVC, nil];
LogbookViewController (subclass of UIViewController)
- (void)viewDidLoad {
[super viewDidLoad];
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
listController.managedObjectContext = self.managedObjectContext;
[navigationController pushViewController:listController animated:NO];
[listController release];
}
The WorkoutListTableViewController (subclass of UITableViewcontroller) currently doesn't have anything in particular in it that I would think would affect it, but here's the ViewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Workouts";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"List" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *fileName = #"short_bg.png";
NSString *filePath = [paths stringByAppendingPathComponent:fileName];
UIImage *paper = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageView *paper_bg = [[UIImageView alloc] initWithImage:paper];
self.tableView.backgroundView = paper_bg;
[fileName release];
[paper release];
//self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
/*UIBarButtonItem *importButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Import" style:UIBarButtonItemStylePlain target:self action:#selector(importDialogAction:)];
self.navigationItem.leftBarButtonItem = importButtonItem;
[importButtonItem release];*/
}
}
I've tried things like listController.view.frame = CGRectMake(whatever) in the LogbookViewcontroller, or self.tableView.frame - CGRectMake(whatever) in the WorkoutListTableViewController, but nothing has worked. Any ideas? I can post more of the code if that helps.
You should be added the Navigation Controller directly to the UITabBarController, then it should size appropriately:
tabBarController = [[UITabBarController alloc] init];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:listController];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: navController, secondVC, thirdVC, nil];
You don't need the View Controller in the 1st tab to add a UINavigationController to the view, just go ahead and add the TableViewController as the root of the nav controller and add the nav controller into the 1st tab.
Related
I want to create a tabBar in my app but only in a detailView not in the AppDelegate. I am searching how to do this in google but everything what I have figured out is in AppDelegate.m
I am trying to do something like this. This shows me a Tab bar with two buttons(without names) but I want to go from one FirstViewController with tabBar, to one of the two items SecondViewController or ThirdViewController with a navigationBar with one backItemButton for get back to the FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupTabBar];
}
- (void) setupTabBar {
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
YPProfileViewController *profile = [[YPProfileViewController alloc] init];
YPProfileViewController *profile2 = [[YPProfileViewController alloc] init];
[localViewControllersArray addObject:profile];
[localViewControllersArray addObject:profile2];
tabBars.tabBarItem = profile2;
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tabBars.view];
}
at least this works for me.
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = #[vc1,vc2];
tabBar.selectedIndex = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];
I have 2 uiviewcontroller
- first one is the rootviewcontroller
- second one is a UIViewController with a mapview inside (MyMap.m/MyMap.h)
My problem is that when I try to show my second view as modalview from the first view my mapview is completely black.
this is the viewdidload of my second view, i try to set as initial view and the mapview works:
- (void)viewDidLoad{
[super viewDidLoad];
mapView.delegate =self;
mapView.showsUserLocation = YES;
UIBarButtonItem *zoomButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom"
style:UIBarButtonItemStylePlain
target: self
action:#selector(zoomIn:)];
UIBarButtonItem *typeButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Type"
style:UIBarButtonItemStylePlain
target: self
action:#selector(changeMapType:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:zoomButton, typeButton, nil];
toolBar.items = buttons;
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = 47.640071;
annotationCoord.longitude = -122.129598;
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = #"Microsoft";
annotationPoint.subtitle = #"Microsoft's headquarters";
[mapView addAnnotation:annotationPoint];
}
and this is my first view action
MyMap *map = [[MyMap alloc]init];
[self presentModalViewController:map animated:YES];
I don't know why my mapview is turning black when i launch it from first view and not when i launch it as initial view any ideas ?
I have added a new file to my project in that i am creating the screens programatically and i used following code to create a grouped table view with a title bar & 2 buttons on title bar, but its creating only grouped table but not title bar y it is so, can any one help me thanx in advance
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Add Item";
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(cancel_Clicked:)] autorelease];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:#selector(save_Clicked:)] autorelease];
self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 415)style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
}
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]
initWithTitle:#"+"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(Add)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
[journeylist.tabBarItem initWithTitle:#"Journey List" image:[UIImage imageNamed:#"listIcon-H.png"] tag:1];
journeylist.navigationItem.title =#"Journey List";
NSArray *controllers = [NSArray arrayWithObjects:journeylist,appstore,settings,about,nil];
self.viewControllers = controllers;
Try this.
Your table frame is CGRectMake(0, 0, 320, 415) so top left, you'll need to leave room for the title bar say CGRectMake(0, 40, 320, 415).
I think you have to present it like this.
SomeViewController *controller = [[SomeViewController alloc]
initWithNibName:#"SomeViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:controller];
navController.navigationBar.tintColor = [UIColor grayColor];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
[controller release];
Edit
When you are showing that controller with table view you have to frist add it to a navigation controller in order to show the navigation bar.
i created TAb bar programatically in this manner
UITabBarController *tabBarController = [[UITabBarController alloc] init];
contacts *vc1 = [[contacts alloc]init];
vc1.tabBarItem.image=[UIImage imageNamed:#"contacts.png"];
search* vc2 = [[search alloc] init];
vc2.tabBarItem.image=[UIImage imageNamed:#"search.png"];
a1* vc3 = [[a1 alloc] init];
a2 *vc4 = [[a2 alloc] init];
a3 *vc5 = [[a3 alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2,vc3,vc4,vc5, nil];
tabBarController.viewControllers = controllers;
[self.view addSubview:detailNavCont];
// Add the tab bar controller's current view as a subview of the window
[self.view addSubview:tabBarController.view];
What i want to accomplish is, i want to assign images to tab bar, firstly i tried on first 2 tabs, is showing a blue block instead of image.
secondly, when we create tab bar through Interface builder, there are custom tab bar item, like, contacts, search, bookmark ,compose etc.
so, if i want to assign contacts or search image to my tab bar items,which looks like the one in IB, how can i do it??
regards
Try something like this
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
HomeTabViewController *ptr_homeTab;
ptr_homeTab = [[HomeTabViewController alloc]initWithNibName:#"HomeTabViewController" bundle:nil];
UINavigationController *homeNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_homeTab];
homeNavBar.tabBarItem.title=#"Home";
homeNavBar.tabBarItem.image=[UIImage imageNamed:#"home.png"];
[ptr_homeTab release];
myHospitalviewController=[[MyHospitalViewController alloc]initWithNibName:#"MyHospitalViewController" bundle:nil];
UINavigationController *myHospitalNavBar=[[UINavigationController alloc]initWithRootViewController:myHospitalviewController];
myHospitalNavBar.title=#"My Hospital";
myHospitalNavBar.tabBarItem.image=[UIImage imageNamed:#"myhospital.png"];
[myHospitalviewController release];
viewController = [[TreatMentiViewController alloc]initWithNibName:#"TreatMentiViewController" bundle:nil];
UINavigationController *hospitalNavBar=[[UINavigationController alloc]initWithRootViewController:viewController];
hospitalNavBar.tabBarItem.title=#"Hospital";
hospitalNavBar.tabBarItem.image=[UIImage imageNamed:#"hospital.png"];
[viewController release];
PersonalMedicineViewController *ptr_PersonalMedicine = [[PersonalMedicineViewController alloc] initWithNibName:#"PersonalMedicineViewController" bundle:nil];
UINavigationController *managerNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_PersonalMedicine];
managerNavBar.tabBarItem.title=#"Manager";
managerNavBar.tabBarItem.image=[UIImage imageNamed:#"manager.png"];
[ptr_PersonalMedicine release];
[localViewControllersArray addObject:homeNavBar];
[localViewControllersArray addObject:hospitalNavBar];
[localViewControllersArray addObject:myHospitalNavBar];
[localViewControllersArray addObject:managerNavBar];
[homeNavBar release];
[hospitalNavBar release];
[myHospitalNavBar release];
[managerNavBar release];
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[localViewControllersArray release];
[window addSubview:tabBars.view];
Your images need to have a proper mask. You should save as png.
I am displaying a UITableViewController inside of a UITabBarController that is being presented modally:
-(IBAction)arButtonClicked:(id)sender{
//this is a uitableviewcontroller
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
arTabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arViewController, lbViewController, nil];
arTabBarController.selectedViewController = arViewController;
[self presentModalViewController:arTabBarController animated:YES];
}
In my viewDidLoad for arViewController method I am setting the navigation items:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;
self.title = #"AR";
leaderBoardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:#selector(leaderBoardButtonClicked:)];
self.navigationItem.rightBarButtonItem = leaderBoardButton;
}
My navigation bar doesn't appear when it is inside of the UITabBarController, but when I push the view itself I am able to see it.
What am I missing?
Heh, I've been stumped by this too. What you need to do is send the rootViewController.
I've never used a tabBar for anything except on the main screen but ur code will probably look like this:
after arTabBarController.selectedViewController = arViewController;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController: arTabBarController] autorelease];
[self presentModalViewController: navController animated:YES];
Like I said I haven't done it with a tabBar but I'm pretty sure it will be something along these lines
I needed to add a UINavigationBar:
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];
arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
arTabBarController.selectedViewController = arNavController;
[self presentModalViewController:arTabBarController animated:YES];
There is a simple solution, put setting in view will appear
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
hope it help some newbies;