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];
}
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())
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.
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)
I just modified the tableViewHeight (currently hardcoded for testing), but will calculate based on size of tabbar and navigationbar (since navigation controller)
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.
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
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];
}
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())