How to position a TabBar programmatically - iphone

I am creating a view controller programmatically which is a UITabBarDelegate and contains a UITabBar.
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 200, 320, 49)];
[self.view addSubview:tabBar];
I don't really want to hardcode the frame values in case the screen resolution changes or I use the view in a different context with a different height, for example.
I'm vaguely aware I can create the tab bar without these values and probe the window and tabbar for height and width to calculate the desired values but I'm struggling to achieve this. What I want to achieve is a tab bar of the standard width and height correctly positioned at the bottom of the enclosing view.

Maybe something like this:
UITabBar *tabBar = [[UITabBar alloc] init]; //init with default width and height
tabBar.frame = CGRectMake(0,
self.view.frame.size.height - tabBar.frame.size.height,
self.view.frame.size.width,
tabBar.frame.size.height)]; //place it at the bottom of the view
[self.view addSubview:tabBar];
[tabBar release];
EDIT:
I've realised thet the size of a newly instantiated UITabBar is zero. Probably your best bet is to use a UITabBarController, as it already sizes your UITabBar accordingly:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[self.view addSubview: tabBarController.view];

Related

UIBarButtonItem not shown in subview

I want to create a subview with a NavigationBar and a TableView. In the Navigation Bar should be a button which can edit the tableView. The navigationBar and the TableView is shown in the subview but the barButtonItem not. Is there an error in my code, or is there a problem because it is a subview ?
UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(0, 325, screenSize.size.width, 200)];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(-10, 40, screenSize.size.width, 150) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[tagView addSubview:tableView];
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, tagView.frame.size.width, 45)];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[tagView addSubview:navigationBar];
[self.view addSubview:tagInstruction];
[self.view addSubview:tagView];
self.navigationItem refer to navigationController navigation item and you don't have any navigation controller, you have only custom NavigationBar and hence its not working.
For solving this problem you have to create your own UIButton and add in the subview of NavigationBar.
The navigationItem represents the view controller in a parent’s navigation bar, not in the navigationBar you are constructing here. Put the Button into your navigationBar.
If I need a clean navigationBar I always put my UIViewControllers in a UINavigationController. It does not hurt and you can add navigation functionality later. Then you can use self.navigationItem to add a UIBarButtonItem
Like Sven said, the self.navigationItem refers to the UINavigationItem for the UINavigationBar which belongs to the view controller's parent UINavigationController. If you want to set your button in the navigation bar you just alloc'd, try this:
UINavigationItem * navigationItem = [[UINavigationItem alloc] init];
navigationItem.leftBarButtonItem = self.editButtonItem;
[navigationBar setItems:#[navigationItem]];

UIPIckerView in UIPopoverController

I'm not trying to resize the PickerView's height. I'm fine with having the default size, which I believe is 320 x 216. I created this code to present a pickerView in my popovercontroller, however, I get these messages on the console:
2
011-06-30 13:18:28.125 MiGenome[64357:207] -[UIPickerView setFrame:]: invalid height value 1024.0 pinned to 216.0
2011-06-30 13:18:28.126 MiGenome[64357:207] -[UIPickerView setFrame:]: invalid height value 448.0 pinned to 216.0
2011-06-30 13:18:28.127 MiGenome[64357:207] -[UIPickerView setFrame:]: invalid height value -16.0 pinned to 162.0
I don't know why I get this since I'm trying to use the picker default size in the popover. Here's my code. Thanks.
- (IBAction)presentSortPopover {
UIViewController *sortViewController = [[UIViewController alloc] init];
UIPickerView *sortPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, sortViewController.view.bounds.size.width, sortViewController.view.bounds.size.height)];
sortViewController.view = sortPickerView;
sortViewController.contentSizeForViewInPopover = CGSizeMake(320, 216);
sortPickerView.delegate = self;
sortPickerView.dataSource = self;
sortPickerView.showsSelectionIndicator = YES;
self.SortPopover = [[UIPopoverController alloc] initWithContentViewController:sortViewController];
[self.SortPopover presentPopoverFromRect:_sortButtonPop.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[sortPickerView release];
[sortViewController release];
}
I had the same problem until I embedded the UIPickerView in a generic UIView with the same dimensions (0, 0, 320, 216), and set the view controller's view property to the UIView.
Also, I used the setPopoverContentSize:animated: method on the UIPopoverViewController to set the popover dimensions, rather than setting it on the UIViewController.
Hope that helps.
I think this line is the culprit.
UIPickerView *sortPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, sortViewController.view.bounds.size.width, sortViewController.view.bounds.size.height)];
try
UIPickerView *sortPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
UIViewController has a method called contentSizeForViewInPopover.
If you set the expected size for your view controller using it (using a CGSize) you will be able to prevent the UIPopoverController from resizing itself wrong.

How can I add a UIView above viewable area of a UITableView?

I understand that there is a tableHeaderView property, but when ever I add my view to that, it is not hidden above the scroll area.
What I would like to have is, my custom view shown when you pull down the tableview and hold and you see my UIView brought into view. This is done on many apps to put a logo or such slightly hidden until a user pulls down on the tableview (Twitter/Facebook when you pulldown).
I am currently using the following and it is not putting it out of the view:
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
l.text = #"Hidden Text";
l.textColor = [UIColor redColor];
self.tableView.tableHeaderView = l;
[l release];
Since UITableView is actually a UIScrollView with some extra functionality, you can use contentInset to obtain the effect you want. The trick is to use a negative value for the top inset. This will normally hide your header view, but it will still be viewable when the table bounces.
So, after you add the label to the header view, just set the contentInset like this:
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
l.text = #"Hidden Text";
l.textColor = [UIColor redColor];
self.tableView.tableHeaderView = l;
//add this
[self.tableView setContentInset:UIEdgeInsetsMake(-l.bounds.size.height, 0.0f, 0.0f, 0.0f)];
[l release];
The best solution here is to add your view to the header, as you had mentioned you tried, and then in your controller's viewDidLoad actually scroll the tableview downward programmatically until the header view you wanted hidden is hidden. This can be done a number of different ways. The easiest is probably:
[self.tableView setContentOffset: CGPointMake(0, myHeaderHeight)];
Simply have a 0-height header view, and then have a subview of that be positioned with a negative y, and so that the bottom edge of the subview is the top of the view.
UIWindow* window = [[UIApplication sharedApplication].delegate.window;
[window addSubview: your-overlayview];

iPhone: Anchor UINavigationBar to bottom of screen

I am creating a UINavigationBar which works fine. But, it scrolls with my UITableView, which I don't want. How can I anchor the navbar at the bottom of the screen, so that it does NOT scroll?
bottomNav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height, self.view.frame.size.width, 44.0)];
bottomNav.barStyle = UIBarStyleBlackOpaque;
[self.view addSubview:bottomNav];
It sounds like your adding it to the tableview instead of view that contains the tableview. If self is a tableview controller that is definitely your problem.
Yes, TechZen you were right. I needed to add it to my parent view controller:
bottomNav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - 20, self.view.frame.size.width, 44.0)];
bottomNav.barStyle = UIBarStyleBlackOpaque;
[self.parentViewController.view addSubview:bottomNav];

Help Fixing UIViewController with a UIToolbar

I have an app with a navigation bar at the top. On one view that is a subclass of UITableView, I add a UIToolbar beneath the UITableView with the following code:
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit]; // Set the toolbar to fit the width of the app
CGFloat toolbarHeight = [toolbar frame].size.height; // Calculate the height of the toolbar
CGRect rootViewBounds = self.parentViewController.view.bounds;
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
CGRect rectArea = CGRectMake(0, toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
[self.navigationController.view addSubview:toolbar];
The problem is that the toolbar is "on top" of the UITableView and is masking over the top of the contents of the first row in the UITableView. What I really want is for the table view to "start" beneath the UIToolbar.
How do I make this work appropriately?
Gracias,
Jose
I agree with nonamelive, but if there is a reason to need the logic as you described. You should make the view a plain view with both a UITableView subview and a toolbar subview and set their frames accordingly to position them as you want.
Another option if you wish for the toolbar to scroll along with the other table content is to make the table's headerView the toolbar.
In my opinion, initializing a new toolbar is just the wrong way to go.
Just use this simple code since you already have an UINavigationController.
- (void)viewDidLoad {
self.navigationController.toolbar.hidden = NO;
}
I'm sorry for the wrong code. Try this instead!
- (void)viewDidLoad {
self.navigationController.toolbarHidden = NO;
}