页面滑动悬停在某个控件(两种做法)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [scrollView addSubview:[[UISwitch alloc] init]];
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 1500);
    scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:scrollView];
    
    UIView *bar = [[UIView alloc] init];
    bar.frame = CGRectMake(0, 70, self.view.frame.size.width, 50);
    bar.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
    [scrollView addSubview:bar];
    self.bar = bar;
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 70) {
//        self.bar.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
//        [self.view addSubview:self.bar];
        self.bar.frame = CGRectMake(0, scrollView.contentOffset.y, self.view.frame.size.width, 50);
    } else {
//        self.bar.frame = CGRectMake(0, 70, self.view.frame.size.width, 50);
//        [scrollView addSubview:self.bar];
    }
}
原文地址:https://www.cnblogs.com/yintingting/p/4689770.html