0

Is it possible to disable the scrolling of tableHeaderView (Not to be confused with section header).Right now whenever I scroll the table, view in the tableHeaderView also gets scrolled.

What i am doing:

  1. I have a class subclassed from UITableViewController.
  2. In storyboard, I am using the static table view.
  3. Table style is Grouped and I have added 8 sections having a row each.
  4. On the top of 1st section, added a view which is the tableHeaderView.

enter image description here

enter image description here

I want to disable the scrolling of view with title "Profile" when I scroll the table.

PS: I know this is achievable if I subclassed my class from UIViewController instead of UITableViewController. But I don't want to UIViewController because I am using storyboard for designing static cell and If I use UIViewController instead of UITableViewController then compiler throws a warning "Static table views are only valid when embedded in UITableViewController instances"

Please let me know which is the best approach to achieve this.Is it possible to disable the scrolling of tableHeader using my current approach or do I need to use UIViewController instead.

Apoorv
  • 362
  • 5
  • 12
  • you might find this answer useful... http://stackoverflow.com/a/6961973/1757581 – RTasche Sep 02 '13 at 06:13
  • @hacker2007 That is one of the approach but it will not work in my case because I am using storyboard for designing static cell and If I use UIViewController instead of UITableViewController then compiler throws a warning "Static table views are only valid when embedded in UITableViewController instances" – Apoorv Sep 02 '13 at 07:08
  • in storyboard editor you could try dragging a view from the object library to the small space between black status bar an the first static tableview cell. (double click your tableview controller first to zoom it) – RTasche Sep 02 '13 at 07:32

2 Answers2

5

Just use an embed segue with a parent UIViewController consisting of a header view and a container view. Embed your UITableViewController in the container view. More specific steps in this answer.

If you want everything in UITableViewController, you can insert your own subview doing something like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.header = [[UIView alloc] init];
    self.header.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44);
    self.header.backgroundColor = [UIColor greenColor];
    [self.tableView addSubview:self.header];
    self.tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
}

and then manipulate the position of the view in scrollViewDidScroll and friends:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.header.transform = CGAffineTransformMakeTranslation(0, self.tableView.contentOffset.y);
}

I say "and friends" because you'd need to take care of the corner cases like scrollViewDidScrollToTop:. scrollViewDidScroll gets called in every display cycle during scrolling, so doing it this way looks flawless.

Community
  • 1
  • 1
Timothy Moose
  • 9,895
  • 3
  • 33
  • 44
  • Yes this is one of the good approach to achieve the same result which I am trying through UITableViewController only.Waiting for a answer through which we can disable scrolling of tableHeaderView.If I didn't find then will accept your answer. – Apoorv Sep 02 '13 at 07:21
  • This way is working, but if table view had some section header, when you scroll, you can see section header will overlay your table header – Nam Vu Oct 29 '13 at 14:57
  • 1
    @ZuzooVn Change your layout such that the header and table views don't overlap. Keep in mind that they may not overlap in the storyboard, but if you run on a device with a different screen size, the layout can change. You've got to set up your struts & springs or constraints to deal with this. – Timothy Moose Oct 29 '13 at 16:14
  • iO8 - couple of small changes to keep tableview section headers from floating over the headerView – aumansoftware Sep 30 '14 at 23:33
  • At this point, it's probably easier to not use a UITableViewController and add the view manually above the UITableView. – LightningStryk Oct 17 '14 at 16:39
  • Hi @TimothyMoose, I'm trying out your way in is 8 with Swift but for some reason, the header view doesn't even appear! I uploaded a demo Xcode project to my Dropbox [here](https://www.dropbox.com/s/r9dahzyumlgv8f1/FixedHeaderView.zip?dl=0). Can you kindly take a look? – Isuru Dec 04 '14 at 15:48
  • 1
    @Isuru give your header view a non-zero width – Timothy Moose Dec 04 '14 at 19:10
1

Timothy Moose was spot on. Here are the necessary changes for iOS8.

MonoTouch (C#)

// create the fixed header view 
headerView = new UIView() {
                    Frame = new RectangleF(0,0,this.View.Frame.Width,44),
                    AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
                    BackgroundColor = UIColor.DarkGray 
                };
// make it the top most layer
headerView.Layer.ZPosition = 1.0f;

// add directly to tableview, do not use TableViewHeader
TableView.AddSubview(headerView);

// TableView will start at the bottom of the nav bar
this.EdgesForExtendedLayout = UIRectEdge.None;

// move the content down the size of the header view
TableView.ContentInset = new UIEdgeInsets(headerView.Bounds.Height,0,0,0);

.....

[Export("scrollViewDidScroll:")]
public virtual void Scrolled(UIScrollView scrollView)
{
            // Keeps header fixed, this is called in the displayLink layer so it wont skip.
           if(headerView!=null) headerView.Transform = CGAffineTransform.MakeTranslation(0, TableView.ContentOffset.Y);


}

[Export ("scrollViewDidScrollToTop:")]
public virtual void ScrolledToTop (UIScrollView scrollView)
{
            // Keeps header fixed, this is called in the displayLink layer so it wont skip.
            if(headerView!=null) headerView.Transform = CGAffineTransform.MakeTranslation(0, TableView.ContentOffset.Y);


}
aumansoftware
  • 845
  • 8
  • 6