Swift: Fixed header on a Tableviewcontroller

Asked

Viewed 81 times

0

My problem is this: my application has a screen that uses the Uitableviewcontroller layout. I created a header (View) for this screen, however, the header does not get fixed at the top of the screen. That is, when I drag the screen up/down, the header also moves up/down. My question is is is there any way to keep a subView fixed at the top of the screen, this subView is in Uitableviewcontroller?

ps.: I tried this solution but it didn’t work.

override func scrollViewDidScroll (scrollView: UIScrollView)  {
    var fixedFrame: CGRect = self.uiTopView.frame;
    fixedFrame.origin.y = scrollView.contentOffset.y;
    self.uiTopView.frame = fixedFrame;
}
  • You have two options: if your table has only one section, try setting the style of your table as Plain and create a UIView customized. If you have multiple sections, it is not possible with a UITableViewController, then you’ll need to have one UIViewController and separately create your header and table.

2 answers

1

Use a Uiviewcontroller instead of a Uitableviewcontroller.

Create your screen with your view that you want to leave fixed and your tableview right below.

In his Uiviewcontroller also inherit a Uitableviewdelegate and a Uitableviewdatasource. Place your tableview via Iboutlet on your Viewcontroller and have your tableview receive the datasource and Delegate from your class, for example:

self.tableView.datasource = self self.tableView.delegate = self

This way the "Scrollable" area will only be your tableView and not your entire view.

I hope I’ve helped in some way ;)

-1

Breno, try this on:

override func scrollViewDidScroll (scrollView: UIScrollView)  {
    self.uiTopView.frame.origin.y = max(0.0, self.scrollView.contentOffset.y)
}

Don’t forget to add delegate to scrollView

Browser other questions tagged

You are not signed in. Login or sign up in order to post.