Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-30-2008, 07:53 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 100
varchar is on a distinguished road
Default UITabBar - Set to fixed when using table scrolling

Hello,

Recently, I decided to stop using Interface Designer and layout the views on my own. (seems that this methodology is quicker and give more control).

Anyhow, I am having a small problem, which I am sure it is due to me being amateur in this area.

I have a view which has a table and I want to add a UITabBar control to the bottom (naturally).

The table gets populated from other sources, and all works well.

When I try to add the UITabBar, it gets added, BUT: Since the tab is scrollable, the tab bar seems to scroll up with it and is not fixed.

So basically my question is, how can I get the tabbar to be fixed.

Here is excerpt of code:


LoadView Method

Code:

- (void)loadView
{
	// create and configure the table view
	myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];	
	myTableView.delegate = self;
	myTableView.dataSource = self;
	myTableView.autoresizesSubviews = YES;
	
	self.view = myTableView;
	

// I am sure there has to be a way to do below better. But Anyhow, I think below is where we need to set the tabbar to be fixed	
	tabBar = [UITabBar new];

	[self createTabBarItems];

	[tabBar sizeToFit];
	tabBar.autoresizesSubviews = YES;
	
	CGFloat toolbarHeight = [tabBar frame].size.height;
	CGRect mainViewBounds = self.view.bounds;
	[tabBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
								 CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight * 2.0) + 2.0,
								 CGRectGetWidth(mainViewBounds),
								 toolbarHeight)];
	
	[self.view addSubview:tabBar];
	
	
	
}
Create Tab Bar Items

Code:
- (void)createTabBarItems
{		
	UITabBarItem *favorites = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemFavorites tag:0];
	
	NSArray *items = [NSArray arrayWithObjects: favorites, nil];
	[tabBar setItems:items animated:NO];
	
	[favorites release];
}

While I am on this subject, another mini question: What method/property to do I use to tell the tabbaritem to call a method when touched. For example, when the favorites bar item is touched, then i want to call a method (lets call it addToFavorites())

Thanks for help!
varchar is offline   Reply With Quote
Old 12-30-2008, 08:29 PM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

The problem is due to the fact that you are making the table view the view controller's view. So the tab view is now a subview of the table view - oops.

Instead of overloading 'loadView', overload 'viewDidLoad' instead. Now make both the table view and the tab view subviews of 'self.view'. Then just make sure their respective frames are set properly so they touch with no overlap.
RickMaddy is offline   Reply With Quote
Old 01-04-2009, 10:10 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 100
varchar is on a distinguished road
Default

RickMaddy,

Thanks for the guide.

Works great!

I created a view, added tableview to the mainview, and then added the tabview to the mainview. Works wonderful!

Here is code (for others to learn too if they need to):

Code:
- (void)viewDidLoad {
    [super viewDidLoad];

	// Create mainview
	UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];	
	self.view = mainView;
	
	// create and configure the table view
	
	CGFloat tableViewHeight = 480; // todo: get this from const
	CGRect mainViewBounds = self.view.bounds;
	CGRect tableFrame;
	
	tableFrame = CGRectMake(CGRectGetMinX(mainViewBounds),
							CGRectGetMinY(mainViewBounds),
							CGRectGetWidth(mainViewBounds),
							tableViewHeight);
	
//	myTableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];	
	myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];	

	myTableView.delegate = self;
	myTableView.dataSource = self;
	myTableView.autoresizesSubviews = YES;

	[self.view addSubview:myTableView];
	
	
	// Create the tabBar	
	[self createTabBar];
}
A few quick questions, if I may:

1) When scrolling the table all the way to the bottom, the screen jumps back to the top (hard to explain).
2) How do I assign a action to one of the tabbaritems programmatically? (sorry this is a amateur question)

Thanks!
varchar is offline   Reply With Quote
Old 01-04-2009, 10:17 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 100
varchar is on a distinguished road
Default

Answering my question #1:

I just modified the tableViewHeight (currently hardcoded for testing), but will calculate based on size of tabbar and navigationbar (since navigation controller)

Code:

	CGFloat tableViewHeight = 380;
varchar is offline   Reply With Quote
Old 01-04-2009, 03:30 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 100
varchar is on a distinguished road
Default

I answered question 2 (how to get tabbar item action)

Basically just make a delegate:

Code:
 

@interface MyViewController : UIViewController <UITabBarDelegate>
Then implement the delegate method (didSelectItem):

Code:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

	switch (item.tag)
	{
		case kFavoritesTabBarItem: 
			// DO OR GOTO whatever;
			break;
		case kItem3:
			break;
		case kitem3:
			[appDelegate gotoSettingsView];
			break;
			
	}
}
varchar is offline   Reply With Quote
Old 01-04-2009, 06:20 PM   #6 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

There is no reason to create 'mainView' in 'viewDidLoad'. By the time 'viewDidLoad' is called, 'self.view' is already an empty view. Your creation of 'mainView' and assigning it to 'self.view' is superfluous.
RickMaddy is offline   Reply With Quote
Old 10-23-2009, 09:20 AM   #7 (permalink)
iPhone20
 
Join Date: Sep 2009
Posts: 140
apiphone is on a distinguished road
Default Tabbar

Hello,

I was reading you post and i'm interested to know how to create a Tabbar on a view. I don't want to use a Tabbar Controller because it is a navigation based app. I got the code you used to create the Tab bar. Do i have to add a tabbar (not a tabbar controller) on my .xib file along with this code?
Do i have to connect any outlate/delegate along with this.

Can you please explain the procedure to add a Tabbar programmatically on a view in detail.

Thanks a lot


Quote:
Originally Posted by varchar View Post
Hello,

Recently, I decided to stop using Interface Designer and layout the views on my own. (seems that this methodology is quicker and give more control).

Anyhow, I am having a small problem, which I am sure it is due to me being amateur in this area.

I have a view which has a table and I want to add a UITabBar control to the bottom (naturally).

The table gets populated from other sources, and all works well.

When I try to add the UITabBar, it gets added, BUT: Since the tab is scrollable, the tab bar seems to scroll up with it and is not fixed.

So basically my question is, how can I get the tabbar to be fixed.

Here is excerpt of code:


LoadView Method

Code:

- (void)loadView
{
	// create and configure the table view
	myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];	
	myTableView.delegate = self;
	myTableView.dataSource = self;
	myTableView.autoresizesSubviews = YES;
	
	self.view = myTableView;
	

// I am sure there has to be a way to do below better. But Anyhow, I think below is where we need to set the tabbar to be fixed	
	tabBar = [UITabBar new];

	[self createTabBarItems];

	[tabBar sizeToFit];
	tabBar.autoresizesSubviews = YES;
	
	CGFloat toolbarHeight = [tabBar frame].size.height;
	CGRect mainViewBounds = self.view.bounds;
	[tabBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
								 CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight * 2.0) + 2.0,
								 CGRectGetWidth(mainViewBounds),
								 toolbarHeight)];
	
	[self.view addSubview:tabBar];
	
	
	
}
Create Tab Bar Items

Code:
- (void)createTabBarItems
{		
	UITabBarItem *favorites = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemFavorites tag:0];
	
	NSArray *items = [NSArray arrayWithObjects: favorites, nil];
	[tabBar setItems:items animated:NO];
	
	[favorites release];
}

While I am on this subject, another mini question: What method/property to do I use to tell the tabbaritem to call a method when touched. For example, when the favorites bar item is touched, then i want to call a method (lets call it addToFavorites())

Thanks for help!
apiphone is offline   Reply With Quote
Reply

Bookmarks

Tags
uitabbarcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 340
9 members and 331 guests
apatsufas, chemistry, lendo, leostc, Leslie80, lzwasyc, MarkC, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:32 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0