[英]What's the best way to add a view under UINavigationBar in iOS7
[英]iOS7: UICollectionView appearing under UINavigationBar
我已经在iOS 7上构建了一段时间了,但是我还没有解决这个问题,我在Storyboard中创建了一些启用了autolayout的视图,并使用标准的UINavigationController
显示。 大多数都很好,但基于UICollectionView
的那些总是将自己放在导航栏下,除非我将半透明度设置为NO
。 我已经尝试过edgesExtended
技巧,但似乎没有解决它,我不一定介意关闭半透明但我想解决它更干净。
FYI如果您的UICollectionView是视图控制器层次结构中的根视图,并且视图控制器将automaticallyAdjustsScrollViewInsets
设置为YES(这是默认值),则contentInset应自动更新。
但是,如果您的滚动视图(或tableview / collectionview / webview btw)是其视图控制器层次结构中的第一个视图,则scrollview的contentInset仅会自动更新。
我经常在我的层次结构中添加一个UIImageView以获得背景图像。 如果这样做,则必须在viewDidLayoutSubviews中手动设置scrollview的边缘插入:
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat top = self.topLayoutGuide.length;
CGFloat bottom = self.bottomLayoutGuide.length;
UIEdgeInsets newInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
self.collectionView.contentInset = newInsets;
}
之前我遇到过这个问题,只需设置带有上边距的集合视图的边缘:
[self.myCollectionVC.collectionView setContentInset:UIEdgeInsetsMake(topMargin, 0, 0, 0)];
topMargin是导航栏的大小,或者您希望集合开始滚动的任何点。
通过这种方式,您的集合视图将开始在导航栏下方滚动,同时它将填满整个屏幕,如果您的导航栏是半透明的,您将看到它。
我在ios 11之后遇到了这个问题,只是将UICollectionView的contentInsetAdjustmentBehavior设置为 :
self.collectionView.contentInsetAdjustmentBehavior = .never
我正在使用swift和xcode 7.3.1。 我通过转到故事板并选择我的导航控制器然后取消选中“扩展边缘”“在顶部吟游诗人下”来解决它。
-(void) viewDidLoad{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO; //added important
}
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat top = self.topLayoutGuide.length;
CGFloat bottom = self.bottomLayoutGuide.length;
UIEdgeInsets newInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
self.collectionView.contentInset = newInsets;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.