我一直在尝试向我的测试项目添加不同的交互,但在添加 Facebook 的帖子状态栏之类的东西时遇到了麻烦,该状态栏位于时间线滚动视图上,并在向下滚动时随滚动视图一起滚动视图,但当您向上滚动时仍停留在导航栏下方。
我一直在创建一个单独的 UIViewController (不是 UIView)并将其作为子视图添加到主 ViewController 中。不过,我不太确定从那里去哪里......新视图如何随滚动视图滚动?我应该使用单独的视图控制器吗?
任何帮助将不胜感激!谢谢!
以下是一些可用于入门的示例代码,只需将其添加到视图控制器即可。它使用一个通用的UIView
对于浮动栏和通用UIScrollView
对于滚动视图,但您可以将其更改为您想要的任何内容。
@interface BNLFDetailViewController () <UIScrollViewDelegate> {
UIScrollView *_scrollView;
UIView *_floatingBarView;
CGFloat _lastOffset;
}
@end
并且在@implementation
add:
- (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;
}
}
你应该把它作为UIView
, not a UIViewController
。 iOS 开发的一般规则是视图控制器占据整个屏幕,视图用于占据部分屏幕的“子视图”(尽管 iPad 的情况较少)。无论哪种方式,一个UIViewController
有自己的生命周期(willAppear
, didAppear
等),这对于浮动栏来说是不需要/想要的,所以它绝对应该是一个UIView
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)