![](/img/trans.png)
[英]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.