RileyDev is searching for a canonical reply.
I modify the font of the navigation bar like so,
init() {
// Initialise Firebase
//FirebaseApp.configure()
// this isn’t the identical as manipulating the proxy straight
let look = UINavigationBarAppearance()
// this overrides all the pieces you could have arrange earlier.
look.configureWithTransparentBackground()
// this solely applies to large titles
look.largeTitleTextAttributes = [
.font : UIFont(name: “Poppins”, size: 38)!,
NSAttributedString.Key.foregroundColor : UIColor(named: “Dark”)!
]
// this solely applies to small titles
look.titleTextAttributes = [
.font : UIFont(name: “Poppins”, size: 20)!,
NSAttributedString.Key.foregroundColor : UIColor(named: “Dark”)!
]
let again = UIBarButtonItemAppearance()
again.regular.backgroundImage = UIImage(named: “arrow.left”)
again.regular.titleTextAttributes = [
.font : UIFont(name: “Poppins”, size: 20)!,
NSAttributedString.Key.foregroundColor : UIColor(named: “Dark”)!
]
look.backButtonAppearance = again
look.shadowColor = .clear
#if !targetEnvironment(macCatalyst)
look.backgroundColor = UIColor(named: “White”)
#endif
//Within the following two traces you just be sure you apply the model for good
UINavigationBar.look().scrollEdgeAppearance = look
UINavigationBar.look().standardAppearance = look
// This property shouldn’t be current on the UINavigationBarAppearance
// object for some motive and you need to depart it til the top
UINavigationBar.look().tintColor = UIColor(named: “Black”)
UINavigationBar.look().prefersLargeTitles = true
UITextView.look().backgroundColor = .clear
UINavigationBar.look().isTranslucent = false
UIToolbar.look().backgroundColor = UIColor(named: “White”)
UIToolbar.look().isTranslucent = false
}
When including the next modifiers to a view, particularly to Map as SwiftUI Map overrides the toolbar appearances for TabBar and NavigationBar.
.toolbarBackground(.hidden, for: .tabBar
.toolbarBackground(.hidden, for: .navigationBar)
This then modifications modifications title attributes of UINavigationBar.look and makes use of the default iOS look.
I’ve tried utilizing [SwiftUI-Introspect][1] to change the navigation bar like so, nevertheless this didn’t work.
.introspect(
.navigationView(model: .stack),
on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17)
) { navigationController in
navigationController.navigationBar.backgroundColor = UIColor.white
navigationController.navigationBar.isTranslucent = false
let navbarAppearance = UINavigationBarAppearance()
navbarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navbarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navbarAppearance.backgroundColor = UIColor.white
navbarAppearance.largeTitleTextAttributes = [
.font : UIFont(name: “Poppins”, size: 38)!,
NSAttributedString.Key.foregroundColor : UIColor(named: “Dark”)!
]
// this solely applies to small titles
navbarAppearance.titleTextAttributes = [
.font : UIFont(name: “Poppins”, size: 20)!,
NSAttributedString.Key.foregroundColor : UIColor(named: “Dark”)!
]
navbarAppearance.shadowColor = .clear
navbarAppearance.shadowImage = UIImage()
UINavigationBar.look().standardAppearance = navbarAppearance
UINavigationBar.look().compactAppearance = navbarAppearance
UINavigationBar.look().scrollEdgeAppearance = navbarAppearance
}
It modifications the font again to default. Why is that this and the way can I modify the font/stop this from taking place?
This can be a observe up from a prev query on why Map overrides the toolbar look. The workaround was utilizing the Toolbar modifier.
Full code:
import SwiftUI
import MapKit
struct RootController: View {
init() {
// Initialise Firebase
//FirebaseApp.configure()
// this isn’t the identical as manipulating the proxy straight
let look = UINavigationBarAppearance()
// this overrides all the pieces you could have arrange earlier.
look.configureWithTransparentBackground()
// this solely applies to large titles
look.largeTitleTextAttributes = [
.font : UIFont(name: “Times”, size: 38)!,
NSAttributedString.Key.foregroundColor : UIColor.red
]
// this solely applies to small titles
look.titleTextAttributes = [
.font : UIFont(name: “Times”, size: 20)!,
NSAttributedString.Key.foregroundColor : UIColor.red
]
let again = UIBarButtonItemAppearance()
again.regular.backgroundImage = UIImage(named: “arrow.left”)
again.regular.titleTextAttributes = [
.font : UIFont(name: “Times”, size: 20)!,
NSAttributedString.Key.foregroundColor : UIColor.red
]
look.backButtonAppearance = again
look.shadowColor = .clear
#if !targetEnvironment(macCatalyst)
look.backgroundColor = UIColor.white
#endif
//Within the following two traces you just be sure you apply the model for good
UINavigationBar.look().scrollEdgeAppearance = look
UINavigationBar.look().standardAppearance = look
// This property shouldn’t be current on the UINavigationBarAppearance
// object for some motive and you need to depart it til the top
UINavigationBar.look().tintColor = UIColor.black
UINavigationBar.look().prefersLargeTitles = true
UITextView.look().backgroundColor = .clear
UINavigationBar.look().isTranslucent = false
UIToolbar.look().backgroundColor = UIColor.white
UIToolbar.look().isTranslucent = false
UITabBar.look().barTintColor = UIColor.white
UITabBar.look().backgroundColor = UIColor.white
UITabBar.look().shadowImage = UIImage()
UITabBar.look().backgroundImage = UIImage()
}
var physique: some View {
TabView {
ContentView()
.tabItem {
Label(“Residence”, systemImage: “home”)
}
NavigationStack {
VStack {
Picture(systemName: “globe”)
.imageScale(.massive)
.foregroundStyle(.tint)
Textual content(“Hey, world!”)
}
.padding()
}
.tabItem {
Label(“Account”, systemImage: “individual”)
}
}
.background(Colour.white) // Set tab view background explicitly
}
}
#Preview {
RootController()
}
struct ContentView: View {
var physique: some View {
@State var place = MapCameraPosition.area(
MKCoordinateRegion(
middle: CLLocationCoordinate2D(latitude: 51.510357, longitude: -0.116773),
latitudinalMeters: 500, longitudinalMeters: 500)
)
NavigationStack() {
ScrollView() {
NavigationLink(vacation spot: MapView(place: $place)) {
Textual content(“See Map”)
.body(maxWidth: .infinity, alignment: .middle)
.padding(.prime, 400)
.padding(.backside, 600)
}
}
.body(maxWidth: .infinity, alignment: .middle)
.navigationTitle(“Content material”)
.navigationBarTitleDisplayMode(.massive)
}
.navigationTitle(“Content material”)
}
}
struct MapView: View {
@Binding var place: MapCameraPosition
var physique: some View {
ZStack() {
ZStack() {
Colour(.white).ignoresSafeArea()
Rectangle().blendMode(.destinationOut)
}
.compositingGroup()
Map(place: $place)
.mapControlVisibility(.hidden)
.edgesIgnoringSafeArea(.backside)
.allowsHitTesting(false)
.body(maxWidth: .infinity)
.body(top: 414)
.clipped()
.padding()
// .toolbarBackground(.white, for: .navigationBar)
}
.toolbarBackground(.hidden, for: .tabBar)
.toolbarBackground(.hidden, for: .navigationBar)
.navigationTitle(“Map”)
.navigationBarTitleDisplayMode(.massive)
}
}