iOS 15系統導航欄適配


熱烈歡迎,請直接點擊!!!

進入博主App Store主頁,下載使用各個作品!!!

注:博主將堅持每月上線一個新app!!!

iOS 15的系統導航欄背景默認靜止時隱藏,得頁面能滑動且有內容經過導航欄區域才會顯示...

 

iOS15默認樣式.GIF

解決方法

iOS 15后,需要手動設置UINavigationBarscrollEdgeAppearancestandardAppearance屬性才行。

// 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實例里面設置其屬性。

如果想統一樣式,scrollEdgeAppearancestandardAppearance都設置同一個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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM