When I create a static table view and put it in a normal view controller, I get an error which says that static table views must be in UITableViewController's.
What I want is a view on the iPad which has a static table view which takes up most of the screen, but a standard UIView under it which always stays visible and has a couple of labels which I update based on content in the table view.
Is there a way to use a UITableViewController and have the tableView not be full screen so that I can add a subview to it in a Storyboard?
If no, can it be done in code?
Any tips would be appreciated!
What you need is a regular UITableViewController
(so you get all the static cell behavior) and then your view as a subview of the table view, but managed such that it always sticks to the bottom of the screen. Apple demonstrated this as WWDC. Here's what I do (in my example, I have a UIDatePicker
that I always want anchored at the bottom):
In viewDidLoad
, create your view and add it as a subview of the table view. Place it at the bottom of the table view.
[self.tableView addSubview:self.datePicker];
[self updateDatePickerBounds];
Implement scrollViewDidScroll
so that you can force your view to the bottom of the screen ever time the table view scrolls:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
// Keep the date picker floating above the table view, anchored to the bottom of the view
[self updateDatePickerBounds];
}
And then implement a function that positions your view correctly:
- (void) updateDatePickerBounds
{
// Keep the date picker floating above the table view, anchored at the bottom
CGRect tableBounds = self.tableView.bounds;
CGRect pickerFrame = self.datePicker.frame;
pickerFrame = CGRectMake(tableBounds.origin.x,
tableBounds.origin.y + CGRectGetHeight(tableBounds) - CGRectGetHeight(pickerFrame),
tableBounds.size.width,
pickerFrame.size.height);
self.datePicker.frame = pickerFrame;
}
Finally, if you don't want your table view going under your bottom view, just set the table view's contentInset
to match the height of your view.
I also call my updateDatePickerBounds
method from viewWillAppear
. I forget why I had to do that.
I am not sure if it works, but you may want to try this approach too:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With