0

I'd like to add a UIView above the table, as if it's the header.

I don't want to use UITableViewHeader because it doesn't allow auto-layout.

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    Don't use UITableViewController - instead use UIViewController then just put a UIView at the top of the scene and underneath but a UITableVIew. – Gruntcakes May 04 '16 at 22:20
  • Adding to @SausageMachine's comment, you could also use a combination of a regular view controller with a container view that contains a UITableViewController. That way you can use static table views and still add your view above the table view. – Duncan C May 04 '16 at 22:22
  • Yes, I'm using a UIViewController , and then adding my tableView programatically. But I don't know what my tableView and header's constraints should be. – TIMEX May 04 '16 at 22:22
  • What does auto layout have to do with whether you add your header as the table view's `headerView` or not? – rmaddy May 04 '16 at 22:26
  • Look at this topic, It will help you http://stackoverflow.com/questions/36926612/swift-how-creating-custom-viewforheaderinsection-using-a-xib-file – iamburak May 04 '16 at 22:30

3 Answers3

0

You can change the UITableView's header view to be a custom view, if you like.

Use it's tableHeaderView property to set it as a custom view. Like so:

tableView.tableHeaderView = YourCustomView()

Or if you don't want to use UITableViewHeader then you can always set tableHeaderView to nil and put a custom view above the table view.

brimstone
  • 3,370
  • 3
  • 28
  • 49
0

You can create a UIView (your header) in your XIB file and set these constraints: header constraints

And set these constrains for your UITableView:

tableview constraints

If you are creating the UITableView programmatically, you can create these constraints programatically by using the same idea above.

Ghulam Ali
  • 1,935
  • 14
  • 15
  • Will scrolling the table view push the uiview upward, filling the screen? – TIMEX May 06 '16 at 06:10
  • Or will the UIView remain there no matter what? – TIMEX May 06 '16 at 06:10
  • No it won't push it upward. It will be fixed on top and the scroll will happen below it. Because we are setting the UITableView constraint to be below the UIView till the bottom of screen so it will only cover that area. – Ghulam Ali May 06 '16 at 07:56
0

You can create UIView above tableView。as if the tableview's cell.then overwrite scrollViewDidScroll() method and change frame when tableview scrolling.

Shawn
  • 1
  • 1