Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Custom navigation bar with list

I made a list similar to the apple one seen below but I really don't like how the UInavigation bar looks like. Ideally I would want it smaller or have it hidden so I could put my own view there.

Apple list view

I've tried to hide it by using the following from apple appearance api

init() { 
    UINavigationBar.appearance().backgroundColor = .green
    UINavigationBar.appearance().isHidden = true
   }

even having the navigation bar like this would be more ideal compared to having the giant title

navigation bar title

but this has no impact, does anyone have any suggestions or ideas how I could customize this to be more how I want it?

like image 211
yawnobleix Avatar asked Aug 03 '19 13:08

yawnobleix


People also ask

How do I customize the navigation bar title in SwiftUI?

To customize a navigation bar title view in SwiftUI, we simply set ToolbarItem of placement type . principal to a new . toolbar modifier.


1 Answers

This is what I have achieved using swiftUI and a bit of UINavigationView, I believe those modifiers will be implemented to swiftUI after beta.

enter image description here

I achieved this by tweaking max's idea about the UINavigationBar's appearance. How can the background or the color in the navigation bar be changed?

Aside from that, I just throw a toggle to the NavigationBar title's view

var body: some View {
    VStack {
        if displayImg {
            Image("dontstarve")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .edgesIgnoringSafeArea(.all)
        } else {
            Image(systemName: "play")
        }
    }
        .navigationBarTitle(Text("Adjustment"), displayMode: .inline)
        .navigationBarItems(trailing:
            HStack {
                Toggle(isOn: $displayImg) {
                    Text("\(self.displayImg ? "on" : "off")")
                        .foregroundColor(Color.white)
                }
    })
}

Below are the only code that isn't SwiftUI. Which I just throw all of them in init():

init() {
    UINavigationBar.appearance().tintColor = .systemGray6
    UINavigationBar.appearance().barTintColor = .systemTeal
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 25)]
}

And yes, remember to set the navigation bar title mode to inline:

    .navigationBarTitle(Text("Home"), displayMode: .inline)
like image 200
Legolas Wang Avatar answered Oct 01 '22 03:10

Legolas Wang