0

I am trying to keep a UIView with a label, segmentedControl and a button at the top of my UITableViewController as it scrolls. I cannot use a ViewController with UITableView because my TableViewCells are static with UITextFields in them. I know this can be done via ViewController and ContainerViewController, however, this makes things complicated with the amount of data that needs to get passed between the views. I am looking for a scrollViewDidScroll solution in swift.

Here is what I have so far. I have the View already created in storyboards (maybe that is part of the problem.) labelView is connected via outlet.

    override func scrollViewDidScroll(scrollView: UIScrollView) {
    var rect = self.labelView.frame
    rect.origin.y = min(0,scrollView.contentOffset.y + scrollView.contentInset.top);
    self.labelView.frame = rect;
}
rmickeyd
  • 1,551
  • 11
  • 13
  • Something like this? http://stackoverflow.com/questions/5441938/adding-ios-uitableview-headerview-not-section-header (tableHeaderView as a builtin property of a tableview) – solenoid Mar 08 '16 at 03:29
  • I suppose that could work. However, my tableView currently has 3 sections each with 1 row. Each section has a Title already. – rmickeyd Mar 08 '16 at 04:07
  • Oh, if they are static, you could just make views in extra cells and override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { making sure the cells cant be selected. – solenoid Mar 08 '16 at 05:15
  • How would this keep the view in an extra cell at the top of the screen at all times? To be clear, I have one view that I want to remain at the stop of the screen regardless of what section I have on the screen in the tableview. – rmickeyd Mar 08 '16 at 20:18

2 Answers2

1

I figured it out.

First I put a dummy blank view where I wanted my view to be. I did this otherwise it sets itself on top the tableview. I am sure you could also off set in code but this seemed easier. Then I created a separate view outside of the tableViewController with my labels and button set up. Connected this view via outlet. Here is the code to add the view and then keep its position when scrolling.

@IBOutlet var labelView: UIView!

override func viewDidLayoutSubviews() {
    self.view.addSubview(labelView)
    labelView.frame.size.width = self.view.frame.size.width
}

override func scrollViewDidScroll(scrollView: UIScrollView) {
    var rect = self.labelView.frame
    rect.origin.y = max(0,scrollView.contentOffset.y + scrollView.contentInset.top)
    self.labelView.frame = rect
}
rmickeyd
  • 1,551
  • 11
  • 13
0

If you have just one section then you may try adding the view in section header. yeah you have to change lot of thing but it will 100% work

Devang Tandel
  • 2,988
  • 1
  • 21
  • 43
  • I need to have multiple sections for functionality reasons. I essentially need one header view for all sections underneath my navigation bar. – rmickeyd Mar 08 '16 at 20:21
  • if you are using storyboard then you may just drag a UIView above prototype cell and will work for you. you can do whatever you want to do. if you don't wont to scroll the view along with tableview the add programatically and set content offset of tableview just below from the view – Devang Tandel Mar 09 '16 at 04:15
  • This doesn't work unless you add the subView in code. – rmickeyd Mar 10 '16 at 04:36
  • have you tried i am using it, no need to add as subview in code, but surely will scroll along with uitableview – Devang Tandel Mar 10 '16 at 09:38
  • if you want it to be static above then, i would personally prefer o use UIViewController instead of UITableviewController – Devang Tandel Mar 10 '16 at 09:39
  • Please see my original question as to why ViewController and UITableView is not a viable option. – rmickeyd Mar 10 '16 at 16:40
  • ohh static cells...sorry for that – Devang Tandel Mar 11 '16 at 12:15