Fixed header to UITableview?

前端 未结 5 788
日久生厌
日久生厌 2020-12-01 09:46

I\'ve got a UITableView that I\'d like to stick a 44px subview on top of. I tried tableViewHeader, but that scrolls with the rest of the table.

5条回答
  •  旧巷少年郎
    2020-12-01 10:22

    As an option it is possible to embed UITableViewController as part of UI into another UIViewController with 'Container View' element (pick one in Interface Builder from the Object library (where all other views are) ).

    This way you can use UITableViewController like ordinary view (in terms of positioning) and compose any layout you need without overwritting existing table view code

    EDIT:

    to further expand my answer, here are the steps to accomplish the described approach:

    1. Open you storyboar in interface builder
    2. Drag'n'drop a new ViewController element to the storyboard from Object Library to add a new controller.
    3. As a child view, drag'n'drop Container View from Object Library and place it anywhere inside the ViewController

    4. Container View creates another view controller and "embedded" segue as a connection. It's save to delete this viewcontroller and to connect the Container View with the required view controller (as per the questions it's UITableViewController)

    5. To connect Container View with UITableViewController just select the container view and control+drag to the UITableViewController - select "Embed" in the popup menu

    Now the controller will display inside the Container View with respect to the container's position and boundaries.

    It's possible to get a link to the embeeded view controller - the system will fire "prepareForSegue" method of the host viewcontroller (created on the step 1 above) and the controller is in segue.destinationViewController property - one can customize it as required. Just make sure to set an identifier to the "embedded" segue in interface builder - this is the same process just like for any other segues.

提交回复
热议问题