Last active
January 8, 2025 22:06
-
-
Save ajbonner/9a6e9a19cf54fc47607883f40c3565bd to your computer and use it in GitHub Desktop.
Customize an IOS Application's Navigation Bar Back Button in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
class AppDelegate: NSObject, UIApplicationDelegate { | |
func application(_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil | |
) -> Bool { | |
customizeNavigationBarAppearance() | |
} | |
func customizeNavigationBarAppearance() { | |
let customAppearance = UINavigationBarAppearance() | |
customAppearance.configureWithOpaqueBackground() | |
let textAttrs: [NSAttributedString.Key : Any] = [ | |
.foregroundColor: UIColor(.white), | |
.font: UIFont(name: "ProximaNova-Medium", size: 16)! | |
] | |
customAppearance.titleTextAttributes = textAttrs | |
customAppearance.largeTitleTextAttributes = textAttrs | |
customAppearance.backgroundColor = UIColor(.systemBlue) | |
customAppearance.shadowColor = .clear | |
let config = UIImage.SymbolConfiguration(font: UIFont(name: "ProximaNova-Light", size: 14)!) | |
let backIndicatorImage = UIImage(systemName: "chevron.left", withConfiguration: config) | |
customAppearance.setBackIndicatorImage(backIndicatorImage, transitionMaskImage: backIndicatorImage) | |
customAppearance.backButtonAppearance.normal.titleTextAttributes = textAttrs | |
// global nav bar image tint, can't override it on button by button basis without wholly custom button | |
UIBarButtonItem.appearance().tintColor = .white | |
let proxy = UINavigationBar.appearance() | |
proxy.prefersLargeTitles = false | |
proxy.scrollEdgeAppearance = customAppearance | |
proxy.compactAppearance = customAppearance | |
proxy.standardAppearance = customAppearance | |
proxy.compactScrollEdgeAppearance = customAppearance | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment