Is it possible to have a fixed uitableview Header while using sections?
There’s no way to maintain the header of a tableView
fixed, but
an useful approach when you need a unique header, is to use a UIViewController
rather than a UITableViewController
, and set the header (UIView
) out from the tableView
.
Something like this:
UITableView with fixed section headers
The headers only remain fixed (floating) when the UITableViewStyle
property of the table is set to UITableViewStylePlain
.
If you have it set to UITableViewStyleGrouped
, the headers will scroll up with the cells (will not float).
UITableview: Fixed Section Headers
You should add separate tableViews instead of sections in same tableView. That way your tableView's frame will be fixed and adding row to either won't push others frame down.
Avoid sticky header view when tableview scrolls with Auto Layout
Currently your table view and your label are siblings inside your UIViewController's view, which means your label is not part of the table view so it won't scroll with it. You can add the label to a UIView, set it's constraints and then set the tableHeaderView
property of the table view. Here's a sample code with some hardcoded values:
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "some text"
label.sizeToFit()
let headerView = UIView()
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.addSubview(label)
tableView.tableHeaderView = headerView
headerView.centerXAnchor.constraint(equalTo: tableView.centerXAnchor).isActive = true
headerView.heightAnchor.constraint(equalToConstant: 80).isActive = true
headerView.widthAnchor.constraint(equalTo: tableView.widthAnchor).isActive = true
label.leftAnchor.constraint(equalTo: headerView.leftAnchor, constant: 50).isActive = true
tableView.tableHeaderView?.layoutIfNeeded()
Fixed header to UITableview?
I finally figured this out right after posting. Figures. :)
Here's what I did, in case others run into the same problem:
Delete the existing
UITableViewController
and its XIB. They're junk. Get really mad while you do.Make a new
UIViewController
subclass with a XIBOpen XIB in IB and add your header stuff and a
UITableView
to theUIView
In the IB Outlets for
UITableView
make sure you connect Delegate and DataSource to your File OwnerIn the header for your view controller, be sure to add
<UITableViewDelegate, UITableViewDataSource>
to implement these protocolsImplement all the regular
UITableView
delegate and data source methods you know and love, but in yourUIViewController
instead of the way you're used to doing it throughUITableViewController
After this things should work.
How to make a sticky footer and header with a tableview?
Don't confuse the section header and footer with the table header and footer.
The section header and footer, in a non-grouped table, are pinned to the top and bottom of the table while you scroll.
But the table header and footer, as you rightly say, are sort of like cells: they are before the first section and after the last section, and they scroll with the table.
If that's not what you want — that is, if there is something you want to show above the table and below the table, all the time — then you need to put them above and below the table view as completely separate views:
thing above
table
thing below
Of course, if you do that, you can't use a UITableViewController. You'll have to have a normal view controller, with the table view as an embedded view and the table view controller as a child view controller.
Related Topics
How to Add a Container View Programmatically
How Do We Create a Bigger Center Uitabbar Item
If No Table View Results, Display "No Results" on Screen
Email Validation on Textfield in iOS
Didreceiveremotenotification: Fetchcompletionhandler: Open from Icon VS Push Notification
iPhone Simulator Suddenly Started Running Very Slow
Disable Autolayout Constraint Error Messages in Debug Console Output in Xcode
Add Swipe to Delete Uitableviewcell
Paging Uiscrollview in Increments Smaller Than Frame Size
iPhone Sdk:How to Play Video Inside a View? Rather Than Fullscreen
Xcode Changes Unmodified Storyboard and Xib Files
Rotate a Uiview Around Its Center But Several Times
Programmatically Get a Storyboard Id
Uitableview with Two Custom Cells (Multiple Identifiers)
Fade Out Scrolling Uitextview Over Image
Ios5 Images Disappear When Scrolling with Webkit-Overflow-Scrolling: Touch
Autolayout Complains About Constraints for 2 Uitextfields with No Borders