0
votes

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.

2
you might find this answer useful... stackoverflow.com/a/6961973/1757581RTasche
@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
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

2 Answers

5
votes

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.

1
votes

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);


}