iOS15
系統下強制了對navigationBar
統一的樣式管理,在應用打開的時候需要進行樣式的默認設置,這樣統一設置后每個導航器的背景、分割線都會變成統一的樣式,如下。
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *app = [UINavigationBarAppearance new];
[app configureWithDefaultBackground];
app.backgroundColor = UIColor.whiteColor;
app.backgroundImage = [UIImage bt_imageWithColor:UIColor.redColor equalSize:100];
app.shadowColor = UIColor.yellowColor;
[UINavigationBar appearance].scrollEdgeAppearance = app;
[UINavigationBar appearance].standardAppearance = app;
}
這里的standardAppearance
為其它Appearance
屬性的默認值,不設置的話在有UIScrollView
的界面進行滑動的時候導航器樣式會改變。其它Appearance
源碼如下:
/// When set and this item is topmost, overrides the hosting navigation bar's standardAppearance. See UINavigationBar.standardAppearance for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *standardAppearance API_AVAILABLE(ios(13.0), tvos(13.0));
/// When set and this item is topmost, overrides the hosting navigation bar's compactAppearance. See UINavigationBar.compactAppearance for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *compactAppearance API_AVAILABLE(ios(13.0));
/// When set and this item is topmost, overrides the hosting navigation bar's scrollEdgeAppearance. See UINavigationBar.scrollEdgeAppearance for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *scrollEdgeAppearance API_AVAILABLE(ios(13.0));
/// When set and this item is topmost, overrides the hosting navigation bar's compactScrollEdgeAppearance. See UINavigationBar.h for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *compactScrollEdgeAppearance API_AVAILABLE(ios(15.0));
導航器個別樣式的設置方法總結
這個時候如果有一個VC
需要將分割線隱藏,或者設置分割線的顏色用以前的方法會失效,這里總結一些導航器個別樣式常用的設置方法。
隱藏分割線
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
if (app == nil) {
app = [UINavigationBar.appearance.standardAppearance copy];
}
app.shadowColor = UIColor.clearColor;
self.navigationItem.scrollEdgeAppearance = app;
self.navigationItem.standardAppearance = app;
return;
}
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
設置分割線顏色,iOS15
分割線高度不能設置
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
if (app == nil) {
app = [UINavigationBar.appearance.standardAppearance copy];
}
app.shadowColor = color;
self.navigationItem.scrollEdgeAppearance = app;
self.navigationItem.standardAppearance = app;
return;
}
[self.navigationController.navigationBar setShadowImage:[UIImage bt_imageWithColor:color size:CGSizeMake(BTUtils.SCREEN_W, height)]];
設置導航器背景圖片
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
if (app == nil) {
app = [UINavigationBar.appearance.standardAppearance copy];
}
[app configureWithDefaultBackground];
app.backgroundImage = bgImg;
self.navigationItem.scrollEdgeAppearance = app;
self.navigationItem.standardAppearance = app;
return;
}
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setClipsToBounds:NO];
設置導航器背景顏色
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
if (app == nil) {
app = [UINavigationBar.appearance.standardAppearance copy];
}
[app configureWithDefaultBackground];
app.backgroundImage = [UIImage bt_imageWithColor:color size:CGSizeMake(BTUtils.SCREEN_W, BTUtils.NAV_HEIGHT)];
app.backgroundColor = color;
self.navigationItem.scrollEdgeAppearance = app;
self.navigationItem.standardAppearance = app;
return;
}
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage bt_imageWithColor:color] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setClipsToBounds:NO];
self.navigationController.navigationBar.backgroundColor=color;
設置導航器透明
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
if (app == nil) {
app = [UINavigationBar.appearance.standardAppearance copy];
}
[app configureWithTransparentBackground];
app.backgroundImage = [UIImage bt_imageWithColor:UIColor.clearColor size:CGSizeMake(BTUtils.SCREEN_W, BTUtils.NAV_HEIGHT)];
app.backgroundColor = UIColor.clearColor;
self.navigationItem.scrollEdgeAppearance = app;
self.navigationItem.standardAppearance = app;
self.navigationController.navigationBar.translucent = YES;
return;
}
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setBackgroundImage:[UIImage bt_imageWithColor:UIColor.clearColor] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setClipsToBounds:YES];
self.navigationController.navigationBar.backgroundColor=UIColor.clearColor;