Is there a way to use .displayInline option for SwiftUI Menu? - swiftui

In UIKit there is a UIMenu.Option .displayInline to display submenus with separator. Is there a way to do this also in SwiftUI?

You can create Menu separators with Divider.
Example:
struct ContentView: View {
var body: some View {
Menu("Hello world!") {
Button("Item 1") {}
Button("Item 2") {}
Divider()
Button("Item 3") {}
Button("Item 4") {}
}
}
}
Result:

Related

NavigationLink in the Page TabView works with an error

Here is the code i am trying to reproduce.
The problem is that when you click on the link, the screen opens without animation and an error is generated in the console.
But if you go to another page Tab and go back, then everything works correctly.
var body: some View {
TabView {
//First Screen
NavigationView {
NavigationLink {
Text("Hello Page 1")
} label: {
Text("Page 1")
}
}
.navigationViewStyle(.stack)
.tag(1)
//Second Screen
NavigationView {
NavigationLink {
Text("Hello Page 2")
} label: {
Text("Page 2")
.foregroundColor(.green)
}
}
.navigationViewStyle(.stack)
.tag(2)
}
.tabViewStyle(.page(indexDisplayMode: .always))
}
To correct this error you need to use individual structs in your TabView. In a real app, you would be doing this as a matter of course, and would not ever see this error. An example would be:
struct MyTabView: View {
var body: some View {
TabView {
Content1()
.tag(1)
Content2()
.tag(2)
}
.tabViewStyle(.page(indexDisplayMode: .always))
}
}
struct Content1: View {
var body: some View {
NavigationView {
NavigationLink {
Text("Hello Page 1")
} label: {
Text("Page 1")
}
}
.navigationViewStyle(.stack)
}
}
struct Content2: View {
var body: some View {
NavigationView {
NavigationLink {
Text("Hello Page 2")
} label: {
Text("Page 2")
}
}
.navigationViewStyle(.stack)
}
}
NavigationView would go in each of these contained structs as they would be the top of the hierarchy for each tab.

SwiftUI on iPadOS - Multitasking does not show the correct navigation title

This is on iPadOS 15.4. I'm using Xcode 13.3.1. I'm using SwiftUI. I have two files:
iPadNavigationViewTestApp.swift
#main
struct iPadNavigationViewTestApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
List {
NavigationLink(destination: ContentView()) {
Text("Content View 1")
}
NavigationLink(destination: AnotherContentView()) {
Text("Content View 2")
}
NavigationLink(destination: YetAnotherContentView()) {
Text("Content View 3")
}
}
.listStyle(.sidebar)
.navigationTitle("This is a test")
ContentView()
}
}
}
}
ContentView.swift
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
.navigationTitle("ContentView")
}
}
struct AnotherContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
.navigationTitle("AnotherContentView")
}
}
struct YetAnotherContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
.navigationTitle("YetAnotherContentView")
}
}
In the multitasking interface of iPad, the title of the view was not shown correctly:
In the simulator I switched to the second view using the sidebar, and the detail view's navigation title changed to "AnotherContentView". Yet the view title on multitasking is still "ContentView". How do I let it show the correct title? Is this a bug of SwiftUI?

Set Color of Chevron in a SwiftUI List in a NavigationView

I have this list I a NavigationView.
In dark mode the Chevron (red circle) is almost invisible.
How can I set the Color of Chevron in a List.
struct ContentView: View {
var body: some View {
NavigationView{
List {
Line(text: "Line 1")
Line(text: "Line 2")
Line(text: "Line 3",selected: true)
Line(text: "Line 4")
Line(text: "Line 5")
}
}
}
}
struct Line: View {
var text :String
var selected = false
#Environment(\.colorScheme) var colorScheme
var body: some View {
NavigationLink(destination: Text("D")) { Text(text)}
.listRowBackground(selected ? Color.blue : Color(.systemBackground))
.foregroundColor(selected ? Color.white : Color(.label))
.onTapGesture(perform: {print ("tap")
} )
}
}
The standard chevron is not a symbol by raster image, here it is
that is why it does not react on any color changing modifiers.
Solution, disable standard chevron and use own, custom, (behaviour of the List is the same), like below
HStack {
Text(text)
NavigationLink(destination: Text("D")) { EmptyView() } // disabled !
Image(systemName: "chevron.right") // << custom !!
.foregroundColor(Color.red) // any color !!!
}
In my environment, default chevron is shown under customized chevron.
Add opacity(0), it works.
NavigationLink(destination: Text("D")) { EmptyView() }
.opacity(0) // Add this
You can use the accentColor property:
struct ContentView: View {
var body: some View {
NavigationView{
List {
Line(text: "Line 1")
Line(text: "Line 2")
Line(text: "Line 3",selected: true)
Line(text: "Line 4")
Line(text: "Line 5")
}.accentColor(.black)
}
}
}

It is possible to make NavigationView title clickable in SwiftUI?

I want to add action on tap to NavigationView title. Is it possible in SwiftUI?
If you can use the .inline title display mode, you can use the new toolbar API introduced in iOS 14 to overwrite the title displayed in the navigation bar.
See below a simple example, where tapping the title is incrementing the counter:
struct ContentView: View {
#State private var counter = 0
var body: some View {
NavigationView {
VStack {
Text("Main content")
Text("\(counter)")
.font(.title)
}
.navigationBarTitleDisplayMode(.inline)
.toolbar(content: {
ToolbarItem(placement: .principal) {
Text("Press here")
.bold()
.onTapGesture {
counter += 1
}
}
})
}
}
}

How do I hide navigation bar in the tab bar's specific view in SwiftUI?

XCode11 beta3,
MacOS Catalina 10.15 Beta(19A501i)
I want to hide tabBar when push~ Any command will very helpful, Thanks~
Click me to show gif image
:
struct ContentView : View {
var body: some View {
WhenNavigationViewIsRootView()
}
}
struct WhenNavigationViewIsRootView : View {
var body: some View {
NavigationView {
TabbedView{
Rectangle().foregroundColor(.green)
.tag(0).tabItem{Text("Page1")}
VStack {
List {
ForEach(0...2) { i in
NavigationLink(
destination: Text("\(i)"),
label: {Text("\(i)")})
}
}
}.tag(1).tabItem{Text("Page2")}
}
.navigationBarHidden(true)
}
}
}
If you want to hide the navigation bar in a TabbedView, you have to set .navigationBarHidden(true) on the views nested inside TabbedView. This isn't enough, however. For whatever reason, SwiftUI requires that you first set the navigation bar title before you can hide the navigation bar.
NavigationView {
TabbedView{
Rectangle()
.foregroundColor(.green)
.tag(0)
.tabItem{
Text("Page1")
}
.navigationBarTitle("")
.navigationBarHidden(true)
List(0...2) { i in
NavigationLink(destination: Text("\(i)")) {
Text("\(i)")
}
}
.tag(1)
.tabItem {
Text("Page2")
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
}