make UIView in UIScrollView stick to the top when scrolled up

前端 未结 4 1305
春和景丽
春和景丽 2020-12-08 11:49

So in a UITableView when you have sections the section view sticks to the top until the next section overlaps it and then it replaces it on top. I want to have a similar eff

相关标签:
4条回答
  • 2020-12-08 12:07

    Swift Solution based on EVYA's response:

    var navigationBarOriginalOffset : CGFloat?
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationBarOriginalOffset = navigationBar.frame.origin.y
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        navigationBar.frame.origin.y = max(navigationBarOriginalOffset!, scrollView.contentOffset.y)
    }
    
    0 讨论(0)
  • 2020-12-08 12:11

    To create UIView in UIScrollView stick to the top when scrolled up do:

    - (void)createHeaderView:(UIView*)headerView {
        _headerView = [headerView retain];
        _headerViewInitialY = _headerView.frame.origin.y;
        [_scrollView addSubview:_headerView];
        _scrollView.delegate = self;
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        CGRect headerFrame = _headerView.frame;
        headerFrame.origin.y = MAX(_headerViewInitialY, scrollView.contentOffset.y);
        _headerView.frame = headerFrame;
    }
    
    0 讨论(0)
  • 2020-12-08 12:19

    Evya's solution works really well, however if you use Auto Layout, you should do something like this (The Auto Layout syntax is written in Masonry, but you get the idea.):

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
        //Make the header view sticky to the top.
        [self.headerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView.mas_top).with.offset(scrollView.contentOffset.y);
            make.left.equalTo(self.scrollView.mas_left);
            make.right.equalTo(self.scrollView.mas_right);
    
            make.height.equalTo(@(headerViewHeight));
        }];
    
        [self.scrollView bringSubviewToFront:self.headerView];
    }
    
    0 讨论(0)
  • 2020-12-08 12:27

    If I recall correctly, the 2010 WWDC ScrollView presentation discusses precisely how to keep a view in a fixed position while other elements scroll around it. Watch the video and you should have a clear-cut approach to implement.

    It's essentially updating frames based on scrollViewDidScroll callbacks (although memory is a bit hazy on the finer points).

    0 讨论(0)
提交回复
热议问题