带有列表的 SwiftUI 自定义导航栏 [英] SwiftUI Custom navigation bar with list

查看:57
本文介绍了带有列表的 SwiftUI 自定义导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个类似于下面看到的苹果的列表,但我真的不喜欢 UInavigation 栏的样子.理想情况下,我希望它更小或隐藏起来,以便我可以将自己的观点放在那里.

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.

我试图通过使用苹果外观 api 中的以下内容来隐藏它

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?

推荐答案

这是我使用 swiftUI 和一些 UINavigationView 实现的,我相信这些修饰符会在 beta 之后实现到 swiftUI.

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

我通过调整 max 关于 UINavigationBar 外观的想法来实现这一点.背景或导航栏中的颜色要更改吗?

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?

除此之外,我只是切换到 NavigationBar 标题的视图

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)
                }
    })
}

以下是唯一不是 SwiftUI 的代码.我只是将它们全部放入 init() 中:

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)

这篇关于带有列表的 SwiftUI 自定义导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆