SwiftUI 带列表的自定义导航栏
SwiftUI Custom navigation bar with list
我制作了一个类似于下面看到的苹果列表,但我真的不喜欢 UInavigation 栏的样子。理想情况下,我希望它更小或隐藏起来,这样我就可以在那里放置我自己的视图。
我尝试使用以下 apple appearance 来隐藏它 api
init() {
UINavigationBar.appearance().backgroundColor = .green
UINavigationBar.appearance().isHidden = true
}
即使有这样的导航栏也比拥有巨大的标题更理想
但这没有任何影响,有没有人有任何建议或想法,我可以如何自定义它以使其更符合我的要求?
你可以做这样的事情让它像以前的iOS 13 导航栏,然后删除大标题并将它一直放在中间的栏上:
.navigationBarTitle(Text("Landmark"), displayMode: .inline)
这是我使用 swiftUI 和一些 UINavigationView 实现的结果,我相信这些修改器将在 beta 后实现到 swiftUI。
我通过调整 max 关于 UINavigationBar 外观的想法实现了这一点。 How can the background or the color in the navigation bar be changed?
除此之外,我只是切换到 NavigationBar 标题的视图
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():
init() {
UINavigationBar.appearance().tintColor = .systemGray6
UINavigationBar.appearance().barTintColor = .systemTeal
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 25)]
}
是的,记得将导航栏标题模式设置为内联:
.navigationBarTitle(Text("Home"), displayMode: .inline)
我制作了一个类似于下面看到的苹果列表,但我真的不喜欢 UInavigation 栏的样子。理想情况下,我希望它更小或隐藏起来,这样我就可以在那里放置我自己的视图。
我尝试使用以下 apple appearance 来隐藏它 api
init() {
UINavigationBar.appearance().backgroundColor = .green
UINavigationBar.appearance().isHidden = true
}
即使有这样的导航栏也比拥有巨大的标题更理想
但这没有任何影响,有没有人有任何建议或想法,我可以如何自定义它以使其更符合我的要求?
你可以做这样的事情让它像以前的iOS 13 导航栏,然后删除大标题并将它一直放在中间的栏上:
.navigationBarTitle(Text("Landmark"), displayMode: .inline)
这是我使用 swiftUI 和一些 UINavigationView 实现的结果,我相信这些修改器将在 beta 后实现到 swiftUI。
我通过调整 max 关于 UINavigationBar 外观的想法实现了这一点。 How can the background or the color in the navigation bar be changed?
除此之外,我只是切换到 NavigationBar 标题的视图
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():
init() {
UINavigationBar.appearance().tintColor = .systemGray6
UINavigationBar.appearance().barTintColor = .systemTeal
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 25)]
}
是的,记得将导航栏标题模式设置为内联:
.navigationBarTitle(Text("Home"), displayMode: .inline)