SwiftUI Custom navigation bar with list

半腔热情 提交于 2019-12-23 21:28:52

问题


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.

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

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


回答1:


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

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)



回答2:


You can do something like this to make it like previous iOS 13 Navigation Bar, which then removes the large title and places it on the bar in the middle all the time:

 .navigationBarTitle(Text("Landmark"), displayMode: .inline)


来源:https://stackoverflow.com/questions/57338785/swiftui-custom-navigation-bar-with-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!