web-dev-qa-db-ja.com

iOS 11透明ナビゲーションバー

IOS 11では透明なナビゲーションバーを作成できなくなりました。テーブルビューがバーの下に表示されなくなったため、この黒いバーが一番上に表示されます(ストーリーボードのインセットは0から始まるように適切に設定されています)

enter image description here

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
19
Robert Varga

古い:

tableViewを使用している場合は、コードを追加します。

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

新着:

iOS11で自動的に調整されるScrollViewInsetsの変更:

@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)
17
5pers

私は同じ問題に直面し、それを解決することができました。ここに私のために働くものがあります:

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
    }
}

そしてもう1つ、それを機能させるにはまだ必要だと感じました。ほとんどの場合、UICollectionView/UITableView/UIScrollviewはセーフエリアの上部に配置されています。代わりに、この制約をスーパービューの上部に揃えるように変更してください。

enter image description here

以上です。簡単で直感的ではありませんか? Appleに感謝します。

12
Wujo

同様の問題がありました。ストーリーボードのUIViewControllerに対して、「Extended Edges:Under Top/Bottom/Opaque Bar」をtrueに設定しました。 これに似ています また、「 スクロールビューのインセットを自動的に調整 」を無効にすることもできます。

iOS 10と11の間で一貫した動作をさせるには、これをnavigationViewControllerに追加してみてください

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

    if navigationBar.isTranslucent, #available(iOS 11.0, *) {           
        viewController.additionalSafeAreaInsets.top = -navigationBar.bounds.height
    }
}
0
Hogdotmac