iOS 15的系統(tǒng)導(dǎo)航欄背景默認(rèn)靜止時隱藏汹碱,得頁面能滑動且有內(nèi)容經(jīng)過導(dǎo)航欄區(qū)域才會顯示...
iOS15默認(rèn)樣式.GIF
解決方法
iOS 15后粘衬,需要手動設(shè)置UINavigationBar
的scrollEdgeAppearance
和standardAppearance
屬性才行。
// OC
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 15.0, *)) {
UINavigationBar *navigationBar = [UINavigationBar appearance];
UINavigationBarAppearance *scrollEdgeAppearance = [[UINavigationBarAppearance alloc] init];
scrollEdgeAppearance.backgroundColor = UIColor.redColor;
navigationBar.scrollEdgeAppearance = scrollEdgeAppearance;
UINavigationBarAppearance *standardAppearance = [[UINavigationBarAppearance alloc] init];
standardAppearance.backgroundColor = UIColor.greenColor;
navigationBar.standardAppearance = standardAppearance;
}
return YES;
}
// Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 15.0, *) {
let navigationBar = UINavigationBar.appearance()
navigationBar.scrollEdgeAppearance = {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red
return appearance
}()
navigationBar.standardAppearance = {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .green
return appearance
}()
}
return true
}
設(shè)置后的樣式.GIF
從效果上看的出:
- scrollEdgeAppearance:是處于頂部時的背景
- standardAppearance:是滑動后的背景
更多的自定義效果都可以在對應(yīng)的UINavigationBarAppearance
實例里面設(shè)置其屬性。
如果想統(tǒng)一樣式,scrollEdgeAppearance
和standardAppearance
都設(shè)置同一個appearance
即可(不設(shè)置任何屬性則是默認(rèn)的毛玻璃效果):
// OC
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 15.0, *)) {
UINavigationBar *navigationBar = [UINavigationBar appearance];
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
navigationBar.scrollEdgeAppearance = appearance;
navigationBar.standardAppearance = appearance;
}
return YES;
}
// Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 15.0, *) {
let navigationBar = UINavigationBar.appearance()
let appearance = UINavigationBarAppearance()
navigationBar.scrollEdgeAppearance = appearance
navigationBar.standardAppearance = appearance
}
return true
}
以前的樣式.GIF