This question already has answers here:
SwiftUI NavigationLink Button is gray and untouchable
(2 answers)
Closed 3 years ago.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationLink(destination: DetailView()) {
Text("Show Details")
}
}
}
struct DetailView: View {
var body: some View {
Text("Detailed")
}
}
This code gives me a gray text (or button) saying 'Show Details' which is not touchable and does not perform the intended action (navigating to DetailView). Was there a change in the API or is it a bug?
I am using the newest versions of Xcode (Xcode 11 Beta 6 and macOS Catalina 10.15 Beta 6)
Your ContentView should have a NavigationView for NavigationLink to work, and be enclosed in some 'element', here a VStack
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("Show Details")
}
}
}
}
}
Hope this helps!
Related
This question already has an answer here:
how to switch tab programmatically on button click? in swiftui
(1 answer)
Closed 1 year ago.
I know this question has been asked before and I found a few of them but mine is slightly different.
I found this for example:
Programmatically change to another tab in SwiftUI
which works fine if you have the struct ContentView: View {...} and struct FirstView: View {...} on the same Swift file.
But in my project, I have the struct ContentView: View {...} in 1 Swift file and struct FirstView: View {...} in another separate swift file.
for that reason, when I use #Binding var tabSelection: Int in my FirstView() file, I get this error in my ContentView file: Argument passed to call that takes no arguments
Could someone please advice on this issue?
For example, if you try this example it would working! It would not make any deference in result at all if you put them in deferent files!
File ContentView:
import SwiftUI
struct ContentView: View {
#State private var tabSelection = 1
var body: some View {
TabView(selection: $tabSelection) {
FirstView(tabSelection: $tabSelection)
.tabItem {
Text("Tab 1")
}
.tag(1)
SecondView(tabSelection: $tabSelection)
.tabItem {
Text("Tab 2")
}
.tag(2)
}
}
}
File FirstView:
import SwiftUI
struct FirstView: View {
#Binding var tabSelection: Int
var body: some View {
Button(action: { tabSelection = 2 }) { Text("Change to tab 2") }
}
}
File SecondView:
import SwiftUI
struct SecondView: View {
#Binding var tabSelection: Int
var body: some View {
Button(action: { tabSelection = 1 }) { Text("Change to tab 1") }
}
}
I'm new in SwiftUI and I have come to a weird behavior that is driving me nuts. I'm using the following code as an example:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ScrollView {
NavigationLink(
destination: NonEmojiView(),
label: { Text("NON-emoji view.") }
)
Spacer()
NavigationLink(
destination: EmojiView(),
label: { Text("Emoji view.") }
)
}
.navigationTitle("I love emojis")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct EmojiView: View {
var body: some View {
Text("Testing emojis in navigation titles.")
.navigationTitle("♥️")
}
}
struct NonEmojiView: View {
var body: some View {
Text("Testing emojis in navigation titles.")
.navigationTitle("Boring title")
}
}
When I run this app (iOS 14, XCode 12.2) and tap on the Emoji view (second navigation link), and then the "< Back" button in the navigation bar, the inline title style is shown in the navigation bar (image1) instead of the expected one (image 2). Any clue why? Anything am I doing wrong? Is that a SwiftUI bug? The only difference is the emoji in the title.
EDIT: As pointed out by Luffy, if the navigationTitle in ContentView contains an emoji too, it works well. May be related to the height of the title? More points for this to be a SwiftUI bug.
Thanks!!
If you add emoji in a ContentView's navBarTitle, it will work. If you remove it, then it won't. I think it's a SwiftUI bug.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ScrollView {
NavigationLink(
destination: NonEmojiView(),
label: { Text("NON-emoji view.") }
)
Spacer()
NavigationLink(
destination: EmojiView(),
label: { Text("Emoji view.") }
)
}
.navigationBarTitle(Text("I ❤️ emojis"), displayMode: .large) // <~ HERE
}
}
}
struct EmojiView: View {
let emoji = Image(systemName: "heart.fill")
var body: some View {
Text("Testing emojis in navigation titles.")
.navigationBarTitle(Text("❤️"), displayMode: .large)
}
}
struct NonEmojiView: View {
var body: some View {
Text("Testing emojis in navigation titles.")
.navigationBarTitle(Text("Boring title"), displayMode: .large)
}
}
This bug is driving me insane. Sometimes (well most of the time) presented sheet gets dismissed first time it is opened. This is happening only on a device and only the first time the app is launched. Here is how it looks on iPhone 11 running iOS 14.1 built using Xcode 12.1 (can be reproduced on iPhone 7 Plus running iOS 14.0.1 as well):
Steps in the video:
I open app
Swiftly navigate to Details view
Open Sheet
Red Sheed gets dismissed by the system???
I open sheet again and it remains on the screen as expected.
This is SwitUI App project (using UIKIt App Delegate) and deployment iOS 14. Code:
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailsView()) {
Text("Open Details View")
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct DetailsView: View {
#State var sheetIsPresented: Bool = false
var body: some View {
VStack {
Button("Open") {
sheetIsPresented.toggle()
}
}.sheet(isPresented: $sheetIsPresented, content: {
SheetView()
})
}
}
struct SheetView: View {
var body: some View {
Color.red
}
}
I was able to fix this problem by removing line .navigationViewStyle(StackNavigationViewStyle()), but I need StackNavigationViewStyle in my project. Any help will be appreciated.
Updated: There is a similar post on Apple forum with sheet view acting randomly weird.
One solution that I found is to move sheet to the root view outside NavigationLink (that would be ContentView in my example), but that is not ideal solution.
I had the same problem in an app. After a great deal of research, I found that making the variable an observed object fixed the problem in SwiftUI 1, and it seems to be in SwiftUI 2. I do remember that it was an intermittent problem on an actual device, but it always happened in the simulator. I wish I could remember why, maybe when the sheet appears it resets the bound variable?, but this code fixes the problem:
import SwiftUI
import Combine
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailsView()) {
Text("Open Details View")
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct DetailsView: View {
#ObservedObject var sheetIsPresented = SheetIsPresented.shared
var body: some View {
VStack {
Button("Open") {
sheetIsPresented.value.toggle()
}
}.sheet(isPresented: $sheetIsPresented.value, content: {
SheetView()
})
}
}
struct SheetView: View {
var body: some View {
Color.red
}
}
final class SheetIsPresented: NSObject, ObservableObject {
let objectWillChange = PassthroughSubject<Void, Never>()
static let shared = SheetIsPresented()
#Published var value: Bool = false {
willSet {
objectWillChange.send()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Tested on Xcode 12.1, iOS 14.1 in simulator.
Just add your NavigationView view to bellow code line
.navigationViewStyle(StackNavigationViewStyle())
Example :
NavigationView { }.navigationViewStyle(StackNavigationViewStyle())
Problem will be solved (same issues I was faced)
This question already has answers here:
How can I pop to the Root view using SwiftUI?
(31 answers)
Closed 2 years ago.
The screen transitions with NavigationLink.
I want to return to the first screen by clicking the button on the third screen.
How do you do it?
The following code returns from the third screen to the second screen.
struct FirstView: View {
var body: some View {
VStack {
NavigationView {
NavigationLink(
destination: SecondView()
) {
Text("next")
}
}
}
}
}
struct ThirdView: View {
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
VStack {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("next")
}
}
.navigationBarTitle("Third", displayMode: .inline)
}
}
I had encountered this problem, too.
But I solved it already.
Here is the solution.
Maybe it can work.
Check this answer
I guess it might be a bug in beta 3 as the NavigationView is all broken. But a view like that:
struct GenreBadge : View {
#EnvironmentObject var store: Store<AppState>
let genre: Genre
var body: some View {
NavigationLink(destination: MoviesGenreList(genre: genre).environmentObject(store)) {
RoundedBadge(text: genre.name)
}
}
}
is not triggering any push in the navigation stack. The view doens't seems interactive at all. If anyone found a workaround would be good, unless Apple is documenting this behaviour I would consider it broken until beta 4.
There seems to be a bug with the navigation link in Xcode version 11.3(11C29) which I have reported to Apple.
Note: This problem only appears in simulator. It works fine on a real device. Thanks to #djr
The below code works as expect the first time you use the navigation link. Unfortunately it becomes unresponsive the second time around.
import SwiftUI
struct ContentView : View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: SomeView()) {
Text("Hello!")
}
}
}
}
}
struct SomeView: View {
var body: some View {
Text("Detailed View")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Are you actually inside a NavigationView? The following works. But if I missed your point, maybe you can share a little more code.
import SwiftUI
struct ContentView : View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: SomeView()) {
Text("Go!")
}
}
}
}
}
struct SomeView: View {
var body: some View {
Text("Detailed View Here!")
}
}
NavigationLink
A button that triggers a navigation presentation when pressed. This is a replacement for pushViewController
NavigationView {
NavigationLink(destination:
Text("Detail")
.navigationBarTitle(Text("Detail"))
) {
Text("Push")
}.navigationBarTitle(Text("Master"))
}
Or make it more readable by use group destination into it own view DetailView
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Push")
}.navigationBarTitle(Text("Master"))
}
In Xcode 11.3.1, I experienced the same issue but I just had to quit xcode and restart my computer. This apparently fixed an issue (of course its 2021 right now) but I was able to follow apple's swiftui tutorial without any issues. I copied this code and tried it out... worked fine for me. Maybe the issue is your "play" button on the bottom right of your screen was toggled.
Try this:
VStack{
NavigationLink(destination: DetailView()) {
Text("Push")
}.navigationBarTitle(Text("Your Text")).isDetailLink(false)
}
Try this:-
var body: some View {
NavigationView {
NavigationLink(destination: YourView()) {
Text("Go!")
}
}
}