简体   繁体   中英

disable tableHeaderView (Not to be confused with section header) scrolling

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.

在此处输入图片说明

在此处输入图片说明

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.

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.

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


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM