2017-09-24 237 views
15

創建一個透明的導航欄不再適用於ios 11. 我得到這個黑色的酒吧在頂部,因爲表視圖不再在酒吧下面(故事板中的插圖設置正確從0開始) 任何想法爲什麼?ios 11透明導航欄

enter image description here

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) 
self.navigationController?.navigationBar.shadowImage = UIImage() 
self.navigationController?.navigationBar.isTranslucent = true 
+0

您是否嘗試添加self.navigationController?.navigationBar.backgroundColor = UIColor.clear? – Tom

+0

是的,我做到了。相同的結果 –

+0

還是相同的結果 –

回答

13

老:

,如果您使用的tableView,添加代碼:

if (@available(iOS 11.0, *)) { 
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; 
} else { 
    self.automaticallyAdjustsScrollViewInsets = NO; 
} 

新:

自動改變AdjustsScr ollViewInsets在iOS11:

@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets 
API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's 
contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); 
// Defaults to YES 

約contentInsetAdjustmentBehavior:

typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) { 
    UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewContentInset = YES inside a navigation controller, regardless of whether the scroll view is scrollable 
    UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES) 
    UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted 
    UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets 
} API_AVAILABLE(ios(11.0),tvos(11.0)); 

/* Configure the behavior of adjustedContentInset. 
Default is UIScrollViewContentInsetAdjustmentAutomatic. 
*/ 
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0)); 

它可能是iOS11 safeArea的問題。 嘗試這從一個專家的定義:

#define adjustsScrollViewInsets_NO(scrollView,vc)\ 
do { \ 
    _Pragma("clang diagnostic push") \ 
    _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \ 
     if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\ 
      [scrollView performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\ 
     } else {\ 
      vc.automaticallyAdjustsScrollViewInsets = NO;\ 
     }\ 
    _Pragma("clang diagnostic pop") \ 
} while (0) 
7

我有一個類似的問題。我爲故事板中的UIViewController設置了「Extended Edges:Under Top/Bottom/Opaque Bar」。 Like this. 你也可以嘗試禁用 「Automatically Adjusts Scroll View Insets

+0

我已經在頂部和底部檢查過,但不是不透明。檢查所有三個問題解決了問題。 –

4

我面臨同樣的問題,我能解決這個問題。 這裏對我來說是什麼工作:

public override func viewDidLoad() { 
    super.viewDidLoad() 

    self.navigationController?.navigationBar.backgroundColor = UIColor.clear 
    self.navigationController?.navigationBar.isTranslucent = true 
    if #available(iOS 11.0, *) { 
     collectionView.contentInsetAdjustmentBehavior = .never 
    } else { 
     // Fallback on earlier versions 
    } 
} 

還有一件事,我發現還是有必要使其工作。很可能你有你的UICollectionView/UITableView/UIScrollview對齊安全區的頂部。相反,將此約束更改爲與超級視圖頂部對齊。

enter image description here

就是這樣。這不直截了當嗎?謝謝蘋果。