iOS15默认样式.GIFiOS 15的系统导航栏背景默认静止时隐藏,得页面能滑动且有内容经过导航栏区域才会显示...
解决方法
iOS 15后,需要手动设置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
}
设置后的样式.GIF
从效果上看的出:
- scrollEdgeAppearance:是处于顶部时的背景
- standardAppearance:是滑动后的背景
更多的自定义效果都可以在对应的UINavigationBarAppearance
实例里面设置其属性。
如果想统一样式,scrollEdgeAppearance
和standardAppearance
都设置同一个appearance
即可(不设置任何属性则是默认的毛玻璃效果):
// 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
网友评论