简体   繁体   中英

Fix a UIView(from storyboard) to the top of a UITableView

I've attached a UIView from the storyboard to the top of my UITableView and linked it to the code as IBOutlet, and I want it to be fixed to the top of my tableView . I've tried several ways:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    headView.center = CGPointMake(headView.center.x + scrollView.contentOffset.x, headView.center.y + scrollView.center.y);
    [scrollView bringSubviewToFront:headView];
}

and

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect newFrame = headView.frame;
    newFrame.origin.y = 0;

    [headView setFrame:newFrame];
}

and some other ways I found in the site, but none worked for me, I affraid because I connected headView from the storyboard.
How can I do it? many thanks!

Sounds like you have a UITableViewController and have added the view to the table views header view, hence it scrolls with the table.

If this is the case try changing your main view controller to a UIViewController and add the view and tableview there.

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