在Facebook iOS应用程序的时间轴中添加浮动栏到滚动视图

Sop*_*ung 3 objective-c uiscrollview ios

我一直在为我的测试项目添加不同的交互,我无法添加像Facebook的帖子状态栏,位于时间轴滚动视图上的那个,当你向下滚动时滚动滚动视图当你向上滚动时,视图但仍然卡在导航栏下面.

我一直在创建一个单独的UIViewController(不是UIView)并将其作为子视图添加到主ViewController.我不太确定从那里去哪里...新视图如何滚动滚动视图?我是否应该使用单独的viewcontroller?

任何帮助将不胜感激!谢谢!

And*_*der 6

下面是一些可用于开始的示例代码,只需将其添加到视图控制器即可.它使用UIView浮动条的通用UIScrollView和滚动视图的通用,但您可以将其更改为您想要的任何内容.

@interface BNLFDetailViewController () <UIScrollViewDelegate> {
    UIScrollView *_scrollView;
    UIView *_floatingBarView;
    CGFloat _lastOffset;
}
@end
Run Code Online (Sandbox Code Playgroud)

并在@implementation添加:

- (void)viewDidLoad {
    [super viewDidLoad];

    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
    [self.view addSubview:_scrollView];

    CGRect f = self.view.bounds;
    f.size.height = kFloatingBarHeight;
    _floatingBarView = [[UIView alloc] initWithFrame:f];
    _floatingBarView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_floatingBarView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _scrollView) {
        CGFloat offsetChange = _lastOffset - scrollView.contentOffset.y;
        CGRect f = _floatingBarView.frame;
        f.origin.y += offsetChange;
        if (f.origin.y < -kFloatingBarHeight) f.origin.y = -kFloatingBarHeight;
        if (f.origin.y > 0) f.origin.y = 0;
        if (scrollView.contentOffset.y <= 0) f.origin.y = 0; //Deal with "bouncing" at the top
        if (scrollView.contentOffset.y + scrollView.bounds.size.height >= scrollView.contentSize.height) f.origin.y = -kFloatingBarHeight; //Deal with "bouncing" at the bottom
        _floatingBarView.frame = f;

        _lastOffset = scrollView.contentOffset.y;
    }
}
Run Code Online (Sandbox Code Playgroud)

你应该把它做成一个UIView,而不是一个UIViewController.iOS开发中的一般规则是视图控制器占据整个屏幕,视图用于占据屏幕一部分的"子视图"(尽管iPad的情况不是这样).无论哪种方式,a UIViewController拥有它自己的生命周期(willAppear,didAppear等等),这对于浮动条不需要/想要,所以它绝对应该是一个UIView.