Button Click Navigation to DetailView not working in SwiftUI - ios

I have an UI with few buttons. I want to navigate to detail view on button click. For the first time it navigates successfully but when comeback to root view and again I click on the button, I doesn't navigates.
I have followed all the tutorials and I can see the same code which I have used but I am not getting what is the issue occurring.
State of the variable
#State var google: Int? = 0 has been defied in view class struct ContentView: View
Complete code of the root View
struct ContentView: View {
#State var username: String = ""
#State var password: String = ""
#State var selection: Int? = nil
#State var google: Int? = 0
#State var buttton1: Int? = nil
var body: some View {
NavigationView{
VStack{
Spacer()
VStack (alignment: .center, spacing: 20){
Text("Quickly find and book a\n Doctors visit today")
.bold()
.font(.title)
.foregroundColor(.white)
.multilineTextAlignment(.center)
TextField("Email", text: $username)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
TextField("Password", text: $password)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
NavigationLink(destination: DetailView(), tag: 3, selection: self.$buttton1) {
EmptyView();
}
Button(action:{
self.buttton1 = 3;
print("Login");
}){
Text("Sign in")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.green)
.cornerRadius(3)
.foregroundColor(Color.white)
}
Text("Forgot password?")
.foregroundColor(Color.white)
}
.padding(20)
HStack {
VStack { Divider().background(Color.white) }
Text("or")
.foregroundColor(Color.white)
VStack { Divider().background(Color.white) }
}
.padding(20)
VStack(alignment: .leading, spacing: 20){
Button(action:{
print("Search and sign up user");
}){
Text("Search and sign up user")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.white)
.border(Color.white, width: 1)
.cornerRadius(3)
}
NavigationLink(destination: DetailView(), tag: 2, selection: self.$google) {
Button(action:{
self.google = 2;
print("Sign in with Google");
}){
HStack {
/*Image(systemName: "")
.font(.title)*/
Text("Sign in with Google")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.black)
.background(Color.white)
.cornerRadius(3)
}
}
}
/*NavigationLink(
destination: DetailView()){
Text("Click Me")
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.black)
.foregroundColor(Color.white)
}*/
NavigationLink(destination: DetailView(), tag: 1, selection: $selection) {
//EmptyView()
Button(action:{
//self.tag = 1;
self.selection = 1;
print("Sign in with Apple");
},label: {
Text("Sign in with Apple")
})
.frame(minWidth: 0, maxWidth: .infinity,minHeight: 50, maxHeight: 50)
.background(Color.black)
.cornerRadius(3)
.foregroundColor(Color.white)
}
}
.padding(20)
}
.background(Color.blue)
.edgesIgnoringSafeArea(.all)
}
}
}
struct ContentView: View {
#State var username: String = ""
#State var password: String = ""
#State var selection: Int? = nil
#State var google: Int? = 0
#State var buttton1: Int? = nil
var body: some View {
NavigationView{
VStack{
Spacer()
VStack (alignment: .center, spacing: 20){
Text("Quickly find and book a\n Doctors visit today")
.bold()
.font(.title)
.foregroundColor(.white)
.multilineTextAlignment(.center)
TextField("Email", text: $username)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
TextField("Password", text: $password)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
NavigationLink(destination: DetailView(), tag: 3, selection: self.$buttton1) {
EmptyView();
}
Button(action:{
self.buttton1 = 3;
print("Login");
}){
Text("Sign in")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.green)
.cornerRadius(3)
.foregroundColor(Color.white)
}
Text("Forgot password?")
.foregroundColor(Color.white)
}
.padding(20)
HStack {
VStack { Divider().background(Color.white) }
Text("or")
.foregroundColor(Color.white)
VStack { Divider().background(Color.white) }
}
.padding(20)
VStack(alignment: .leading, spacing: 20){
Button(action:{
print("Search and sign up user");
}){
Text("Search and sign up user")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.white)
.border(Color.white, width: 1)
.cornerRadius(3)
}
NavigationLink(destination: DetailView(), tag: 2, selection: self.$google) {
Button(action:{
self.google = 2;
print("Sign in with Google");
}){
HStack {
/*Image(systemName: "")
.font(.title)*/
Text("Sign in with Google")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.black)
.background(Color.white)
.cornerRadius(3)
}
}
}
/*NavigationLink(
destination: DetailView()){
Text("Click Me")
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.black)
.foregroundColor(Color.white)
}*/
NavigationLink(destination: DetailView(), tag: 1, selection: $selection) {
//EmptyView()
Button(action:{
//self.tag = 1;
self.selection = 1;
print("Sign in with Apple");
},label: {
Text("Sign in with Apple")
})
.frame(minWidth: 0, maxWidth: .infinity,minHeight: 50, maxHeight: 50)
.background(Color.black)
.cornerRadius(3)
.foregroundColor(Color.white)
}
}
.padding(20)
}
.background(Color.blue)
.edgesIgnoringSafeArea(.all)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Known bug on XCode and Simulators with SwiftUI that actually works just fine on real devices.
You cannot push twice in a row the same view.
For example, let's say you have 3 rows (#1, #2, #3), each with a NavigationLink, you will not be able to push #1, come back and push #1 again.
If you run #1, #2 and then, #1 again, it works.
Again, it is just on Simulator. Try the same code on a real device, it will be good!
Hope this helps!
UPDATE: The bug is fixed in XCode 11.4. You can now navigate multiple times to the same View! Good job Apple ;)

Related

iOS 15 Keyboard hides textfield

I am trying to create a messaging page layout but the keyboard hides the textfield and moves everything to the the top. I have looked at many tutorials online and replicated them exactly but none of them seem to work.
Here is all the code I have written:
struct MessagingPage: View {
var user: OfficialUserModel
#ObservedObject private var vm = TheUserModel()
#State var screenWidth = UIScreen.main.bounds.width
#State var imageSize = UIScreen.main.bounds.width / 12
#State var text = ""
#State var show = false
#Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView{
ZStack{
VStack{
HStack{
Spacer()
NavigationLink(destination: UserDisplayPage(user: user)) {
HStack{
WebImage(url: URL(string: user.imageURL ))
.resizable()
.aspectRatio( contentMode: .fill)
.frame(width: imageSize, height: imageSize
)
.cornerRadius(imageSize/2)
VStack(alignment: .leading){
Text(user.FullName)
.font(.body)
.fontWeight(.bold)
.foregroundColor(.white)
Text("\(user.City) , \(user.Country)")
.font(.caption)
.fontWeight(.semibold)
.foregroundColor(.white)
}
}
}
Spacer()
HStack{
Button{
presentationMode.wrappedValue.dismiss()
} label: {
Image(systemName: "chevron.down")
.font(.title)
.foregroundColor(.myWhite)
}
}
.padding(.trailing)
}
.padding(.top, 30)
.frame(height: 75)
.background(Color.mainBlack)
ScrollView{
Spacer()
ForEach(0..<2){ num in
HStack{
Spacer()
HStack{
Text("Hello \(user.firstName)")
.foregroundColor(.orange)
}
.padding()
.background(Color.mainBlack)
.cornerRadius(15)
.shadow(color: .orange, radius: 2)
}
.padding(.horizontal)
.padding(.top, 8)
}
HStack{Spacer()}
HStack{
HStack{
Text("\(user.FullName) is unable to recieve your message at this time. Please try again at a later time.")
.foregroundColor(.green)
}
.padding()
.background(Color.mainBlack)
.cornerRadius(15)
.shadow(color: .green, radius: 2)
Spacer()
}
.padding(.horizontal)
.padding(.top, 8)
HStack{Spacer()}
}
.background(Color.mainBlack)
ZStack{
HStack{
HStack{
HStack{
TextField("Say Something...", text: self.$text)
.placeholder(when: text.isEmpty) {
Text("Say Something...").foregroundColor(.myWhite.opacity(0.5))
}
.frame(width: screenWidth - 200, height: screenWidth/25)
.foregroundColor(.myCyan)
.accentColor(.myCyan)
.background(Color.mainBlack)
.textContentType(.emailAddress)
if !text.isEmpty{
Button{
print(text)
self.text = ""
}label: {
Image(systemName: "paperplane")
.foregroundColor(.myCyan)
.font(.system(size: 20))
}
}
else{
Button{
print("Show more options")
}label: {
Image(systemName: "plus")
.foregroundColor(.myCyan)
.font(.system(size: 20))
}
}
}
.frame(width: screenWidth - 150, height: screenWidth/25)
.padding()
.background(Color.mainBlack)
.cornerRadius(30)
.shadow(color: .myCyan, radius: 5)
.padding(.bottom,5)
}
}
.padding(.bottom, 50)
.frame(width: screenWidth)
}
}
}
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(Color.mainBlack)
.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}
I want the the textfield to move up as well as the messages in the scrollview but the top HStack with the user image and back/dismiss button should remain in place.
You really have a lot of code in there - and two reasons to clean it up:
To get a good answer, you need to post a minimal, reproducible example, which may also help you debug the code before posting it
Many lines of code are redundant or useless, for example:
Why using things like HStack{Spacer()} instead of Spacer(), which by the way is not needed in this context?
Why having a stack that has only another stack inside?
Coming to your issue, the problem is that one of the redundant modifiers is preventing the text file to move up, and this is the line to delete:
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
Why setting a frame that is as high and large as much as the screen? This is forcing the text field to stay at the bottom.
I tried to do some clean up, I bet some more can be done - see the code below, now the keyboard does not cover the text field anymore:
struct MessagingPage: View {
// Removed some lines of code to reproduce your issue
#State var screenWidth = UIScreen.main.bounds.width
#State var imageSize = UIScreen.main.bounds.width / 12
#State var text = ""
#State var show = false
#Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView{
// What is the purpose of this ZStack???
// ZStack{
VStack{
HStack{
Spacer()
NavigationLink(destination: Text("Hello")) {
Text("Top bar")
}
Spacer()
HStack{
Button{
presentationMode.wrappedValue.dismiss()
} label: {
Image(systemName: "chevron.down")
.font(.title)
.foregroundColor(.white)
}
}
.padding(.trailing)
}
.padding(.top, 30)
.frame(height: 75)
.background(Color.black)
ScrollView{
// All the views inside the Scrollview need to be inside a VStack
VStack {
// Why this spacer???
// Spacer()
ForEach(0..<2){ num in
HStack{
Spacer()
HStack{
Text("Hello username")
.foregroundColor(.orange)
}
.padding()
.background(Color.black)
.cornerRadius(15)
.shadow(color: .orange, radius: 2)
}
.padding(.horizontal)
.padding(.top, 8)
}
HStack{Spacer()}
HStack{
HStack{
Text("User is unable to receive your message at this time. Please try again at a later time.")
.foregroundColor(.green)
}
.padding()
.background(Color.black)
.cornerRadius(15)
.shadow(color: .green, radius: 2)
Spacer()
}
.padding(.horizontal)
.padding(.top, 8)
// Why a Spacer inside an HStack???
// HStack{Spacer()}
}
.background(Color.black)
}
// What is the purpose of this ZStack???
// ZStack{
// Why an HStack that has only an HStack inside??
// HStack{
// Why an HStack that has only an HStack inside??
// HStack{
HStack{
TextField("Say Something...", text: self.$text)
.frame(width: screenWidth - 200, height: screenWidth/25)
.foregroundColor(.cyan)
.accentColor(.cyan)
.background(Color.white)
.textContentType(.emailAddress)
if !text.isEmpty{
Button{
print(text)
self.text = ""
}label: {
Image(systemName: "paperplane")
.foregroundColor(.cyan)
.font(.system(size: 20))
}
}
else{
Button{
print("Show more options")
}label: {
Image(systemName: "plus")
.foregroundColor(.cyan)
.font(.system(size: 20))
}
}
}
// Are you sure you want set the height of this stack based on the width?
// .frame(width: screenWidth - 150, height: screenWidth/25)
.frame(width: screenWidth - 150)
.padding()
.background(Color.black)
.cornerRadius(30)
.shadow(color: .black, radius: 5)
.padding(.bottom,5)
// }
// }
.padding(.bottom, 50)
.frame(width: screenWidth)
// }
}
// }
// Why setting a frame that is high and large as much as the screen??
// .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(Color.black)
.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}

iOS: How to make my image logo at the top of the page SwiftUI

firstly I am really new to iOS development and Swift (2 weeks coming here from PHP :))
I am building my simple Login page and wondering how to make my Logo image at the top of the page. Also I am wondering if I have done my layout wrong to get the desired layout as in the screenshot. Would appreciate the help on this.
(Logo scribbled out in the screen shot needs to go to the top outside the white background)
Thanks
LoginView:
import SwiftUI
struct LoginView: View {
#State private var email: String = ""
#State private var password: String = ""
let verticalPaddingForForm = 40
var body: some View {
ZStack {
Color(red: 20/225.0 ,green: 22/225.0 , blue: 25/225.0)
VStack(spacing: CGFloat(verticalPaddingForForm)) {
Image("logo")
.resizable()
.scaledToFit()
Divider()
VStack {
TextField("Email", text: $email)
.padding(.horizontal, 30).padding(.top, 20)
Divider()
.padding(.horizontal, 30)
SecureField("Password", text: $password)
.padding(.horizontal, 30).padding(.top, 20)
Divider()
.padding(.horizontal, 30)
}
.background(Color(.white))
Text("Forgotten Password")
.foregroundColor(.blue)
.font(.system(size: 15))
Button(action: /*#START_MENU_TOKEN#*/{}/*#END_MENU_TOKEN#*/) {
Text("Login")
.padding()
.font(.system(size: 20))
}
.background(Color.black)
.foregroundColor(Color.white)
.cornerRadius(10)
.padding(.top, 0)
.padding(.bottom, 20)
}
.padding(.horizontal, CGFloat(verticalPaddingForForm))
.background(Color(.white))
VStack{
Spacer()
Button(action: /*#START_MENU_TOKEN#*/{}/*#END_MENU_TOKEN#*/) {
Text("Register")
.padding()
.font(.system(size: 40))
}
.background(Color(red: 20/225.0 ,green: 22/225.0 , blue: 25/225.0))
.foregroundColor(Color.white)
.cornerRadius(10)
.padding()
}
}.ignoresSafeArea()
};
}
ContentView:
import SwiftUI
struct ContentView: View {
var body: some View {
LoginView()
}
}
extension UIDevice {
var hasNotch: Bool {
let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
return bottom > 0
}
}
Try below code-:
Use a ZStack to give a backgroundColor to your view, and give that a modifier of .ignoresSafeArea().
Use VStack inside ZStack to layout other view components. I have done few modifications on my side.
struct LoginView: View {
#State private var email: String = ""
#State private var password: String = ""
let verticalPaddingForForm = 40
var body: some View {
ZStack() {
Color(red: 20/225.0 ,green: 22/225.0 , blue: 25/225.0).ignoresSafeArea()
VStack(spacing:15){
Image(systemName: “logo")
.resizable()
.scaledToFit()
.frame( height: 200)
.foregroundColor(Color.white)
.padding([.top],15)
VStack(spacing: CGFloat(verticalPaddingForForm)) {
VStack {
TextField("Email", text: $email)
.padding(.horizontal, 30).padding(.top, 20)
Divider()
.padding(.horizontal, 30)
SecureField("Password", text: $password)
.padding(.horizontal, 30).padding(.top, 20)
Divider()
.padding(.horizontal, 30)
}
.background(Color(.white))
.padding([.top])
Text("Forgotten Password")
.foregroundColor(.blue)
.font(.system(size: 15))
Button(action: {}) {
Text("Login")
.padding()
.font(.system(size: 20))
}
.background(Color.black)
.foregroundColor(Color.white)
.cornerRadius(10)
.padding([.bottom])
}
.background(Color(.white))
Spacer()
Button(action: {}) {
Text("Register")
.padding()
.font(.system(size: 40))
}
.foregroundColor(Color.white)
.cornerRadius(10)
}
}
};
}
struct Test1: View {
var body: some View {
LoginView()
}
}
extension UIDevice {
var hasNotch: Bool {
let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
return bottom > 0
}
}
You can use ZStack (alignment: .top) to set the alignment from the top. And then you can use a Spacer() at the end of the second VStack to expand the view.
The following code works for me. It's your code with little changes. Also, I suggest you create a subview to better organize your code, instead of writing all views within the same view.
import SwiftUI
struct LoginView: View {
#State private var email: String = ""
#State private var password: String = ""
let verticalPaddingForForm = 40
var body: some View {
ZStack (alignment: .top){
Color(red: 20/225.0 ,green: 22/225.0 , blue: 25/225.0)
VStack(spacing: CGFloat(verticalPaddingForForm)) {
Image(systemName: "gear")
.resizable()
.scaledToFit()
.frame(width: 100)
Divider()
VStack {
TextField("Email", text: $email)
.padding(.horizontal, 30).padding(.top, 20)
Divider()
.padding(.horizontal, 30)
SecureField("Password", text: $password)
.padding(.horizontal, 30).padding(.top, 20)
Divider()
.padding(.horizontal, 30)
}
.background(Color(.white))
Text("Forgotten Password")
.foregroundColor(.blue)
.font(.system(size: 15))
Button(action: /*#START_MENU_TOKEN#*/{}/*#END_MENU_TOKEN#*/) {
Text("Login")
.padding()
.font(.system(size: 20))
}
.background(Color.black)
.foregroundColor(Color.white)
.cornerRadius(10)
.padding(.top, 0)
.padding(.bottom, 20)
Spacer()
}
.padding(.horizontal, CGFloat(verticalPaddingForForm))
.background(Color(.white))
VStack{
Spacer()
Button(action: /*#START_MENU_TOKEN#*/{}/*#END_MENU_TOKEN#*/) {
Text("Register")
.padding()
.font(.system(size: 40))
}
.background(Color(red: 20/225.0 ,green: 22/225.0 , blue: 25/225.0))
.foregroundColor(Color.white)
.cornerRadius(10)
.padding()
}
}
}
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
Just put Spacer() between Image("logo") and Divider() to push your logo to the top of the screen

Add delete function to Grid layout in swiftUI for iOS

I've managed to build a user form to get inputs and display them in grids. Right now I have them displayed in a scroll view. I'm avoiding lists so as to get a card like appearance. However, I'm not able to get a delete feature to work as it is done with Lists. I'm new to swift and not sure how to implement delete feature in this grid layout. I would like for users to be able to delete entries. I'm using Core Data, Swift and SwiftUI. Thanks.
struct ContentView: View {
// MARK: PROPERTIES
#Environment(\.managedObjectContext) var managedObjectContext
#State private var showingAddChangeView: Bool = false
#State private var dateAdded: Date = Date()
#State private var showingActionSheet = false
#FetchRequest(
entity: Agent.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Agent.dateAdded, ascending: false)]) var cas: FetchedResults<Agent>
let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
return formatter
}()
private var gridItemLayout = [GridItem(.flexible())]
// MARK: BODY VIEW
var body: some View {
NavigationView {
ScrollView {
LazyVGrid(columns: gridItemLayout , alignment: .center, spacing: 30) {
ForEach(self.cas, id: \.self) { caRequest in
VStack(alignment: .leading) {
HStack {
Image(systemName: "calendar")
Text(caRequest.dateAdded ?? "Missing Dates")
.font(.callout).fontWeight(.bold)
Spacer()
Button(action: {
}) {
Image(systemName: "trash.fill")
.foregroundColor(.black)
}
.padding(.trailing, 20)
}
//.padding()
.padding(.leading, 20)
.padding(.top, 25)
Divider().frame(height: 1).background(Color.gray)
.padding()
HStack{
Image(systemName: "paintbrush.fill")
.foregroundColor(Color(.systemGreen))
.padding(.leading, 20)
.padding(.bottom, 5)
Text(caRequest.intention ?? "No Intentions Recorded")
.font(.callout).multilineTextAlignment(.leading)
.padding(.leading, 5)
.padding(.bottom, 5)
}
HStack{
Image(systemName: "checkmark.seal.fill")
.foregroundColor(Color(.systemBlue))
.padding(.leading, 20)
.padding(.bottom, 5)
Text(caRequest.affirmation ?? "No Affirmation Recorded")
.font(.callout).multilineTextAlignment(.leading)
.padding(.leading, 5)
.padding(.bottom, 5)
}
HStack{
Image(systemName: "heart.fill")
.foregroundColor(Color(.systemPink))
.padding(.leading, 20)
.padding(.bottom, 5)
Text(caRequest.gratitudeOne ?? "Missing Gratitude")
.font(.callout).multilineTextAlignment(.leading)
.padding(.leading, 5)
.padding(.bottom, 5)
}
HStack{
Image(systemName: "heart.fill")
.foregroundColor(Color(.systemPink))
.padding(.leading, 20)
.padding(.bottom, 5)
Text(caRequest.gratitudeTwo ?? "Missing Gratitude")
.font(.callout).multilineTextAlignment(.leading)
.padding(.leading, 5)
.padding(.bottom, 5)
}
HStack{
Image(systemName: "heart.fill")
.foregroundColor(Color(.systemPink))
.padding(.leading, 20)
.padding(.bottom, 20)
Text(caRequest.gratitudeThree ?? "Missing Gratitude")
.font(.callout).multilineTextAlignment(.leading)
.padding(.leading, 5)
.padding(.bottom, 20)
}
}
// .frame(width: 350, height: 300)
.frame(minWidth: 0, maxWidth: 350, minHeight: 0, maxHeight: .infinity)
} // END: FOR EACH
} // END: LAZYVGRID
} // END: SCROLLVIEW
.padding(.top, 100)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
HStack {
Image(systemName: "person.crop.square.fill")
.foregroundColor(.white)
.font(.title)
Text("Change Agent")
.foregroundColor(.white)
.font(.title2)
.fontWeight(.bold)
.shadow(radius: 20)
}
}
}
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(
trailing:
Button(action: {
self.showingAddChangeView.toggle()
}) {
Text("Add")
Image(systemName: "plus")
.imageScale(.medium)
} // END: ADD BUTTON
.foregroundColor(.white)
.sheet(isPresented: $showingAddChangeView) {
AddChangeView()
}
.navigationBarItems(trailing: EditButton())
) // END: NAVIGATION BAR ITEMS
} // END: NAVIGATIONVIEW
} // END: BODY VIEW
} // END: STRUCT

SwiftUI Button not being tapped

I Have a button embedded inside a Hstack inside a Vstack inside a ZStack inside a Vstack inside a geometryReader in swiftui that does not get tapped. I put a print statement inside and whenever I tried to Tap the button, the print statement won't print. Can anyone help me out here? Thanks. Heres my code:
struct DetailedGroupView: View {
#State var actrualImage: Image?
#State var title = "title"
#State var description = "description"
#State var sgName = "sgName"
#State var membersCount = 0
#Environment(\.presentationMode) var presentationMode
var body: some View {
GeometryReader{ geo in
VStack{
ZStack{
(self.actrualImage ?? Image("earthPlaceholder"))
.resizable()
.aspectRatio(contentMode: .fill)
VStack{
Spacer()
HStack{
//This button doesn't work
Button(action: {
print("Button Tapped")
self.presentationMode.wrappedValue.dismiss()
}, label: {
Image(systemName: "chevron.left").foregroundColor(.white)
}).padding()
Text(self.title)
.font(.largeTitle)
.fontWeight(.bold)
.multilineTextAlignment(.leading)
.foregroundColor(.white)
.padding()
.minimumScaleFactor(0.5)
Spacer()
}
HStack{
Text(self.description)
.font(.custom("Open Sans", size: 18))
.fontWeight(.ultraLight)
.multilineTextAlignment(.leading)
.foregroundColor(.white)
.padding()
Spacer()
}
Spacer()
HStack{
Image(systemName: "person.2.fill").foregroundColor(.white).padding(.leading)
Text("\(self.membersCount)")
.font(.custom("Open Sans", size: 12))
.fontWeight(.semibold)
.foregroundColor(.white)
Spacer()
Text(self.sgName)
.font(.custom("Open Sans", size: 12))
.fontWeight(.semibold)
.foregroundColor(.white)
.padding()
}.padding()
}.frame(width: geo.size.width, height: 294)
}.frame(width: geo.size.width, height: 294)
.clipShape(RoundedRectangle(cornerRadius: 12))
.edgesIgnoringSafeArea(.top)
Spacer()
ScrollView(showsIndicators: false){
VStack{
Spacer()
}.onAppear{
self.actrualImage = Image("globePlaceholder")
}
}
}
}.navigationBarBackButtonHidden(true)
}
}
Just tested this same code on both XCode 11.5 and 12.0, and the button works fine... be sure to test it on a simulator not on the preview/canvas

Navigation link and scroll view issue SwiftUI

I have a problem with the navigation link and SwiftUI. The navigation bar desapear and my scroll view go up. I have upload a video of the issue on Facebook here the link : the link of the video
This is the code of my main view :
struct MainView: View {
let date = Date()
let calendar = Calendar.current
static let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
return formatter
}()
//MARK: Properties
#ObservedObject var session = FirebaseSession()
let user = Auth.auth().currentUser
#State private var showModal = false
var body: some View {
NavigationView{
// Group {
ZStack {
if session.session != nil {
ScrollView {
VStack{
ScrollView(.horizontal, showsIndicators: false){
HStack{
NavigationLink(destination: RedactorView()) {
RedactorListItem()
}.buttonStyle(PlainButtonStyle())
RedactorListItem()
RedactorListItem()
RedactorListItem()
RedactorListItem()
RedactorListItem()
}//.padding()
}
VStack(alignment: .leading, spacing: 5) {
HStack {
Text("Les derniers médias")
.font(.largeTitle)
.bold()
Spacer()
Text("Voir plus >")
.padding(8)
.background(Color(UIColor.secondarySystemBackground))
.clipShape(Capsule())
}.padding(.horizontal)
ScrollView(.horizontal, showsIndicators: false){
HStack{
NavigationLink(destination: MotherView(viewRouter: ViewRouter())){
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
}.buttonStyle(PlainButtonStyle())
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
}
}
HStack {
Text("Les derniers articles :")
.font(.largeTitle)
.bold()
Spacer()
Text("Voir plus >")
.padding(8)
.background(Color(UIColor.secondarySystemBackground))
.clipShape(Capsule())
}.padding(.horizontal)
ArticleListRow()
ArticleListRow()
ArticleListRow()
}
}
.navigationBarTitle("WYN")
.navigationBarHidden(true)
}.navigationViewStyle(StackNavigationViewStyle()).padding(.top, 90)
ZStack{
VStack{
HStack{
VStack(alignment: .leading) {
VStack {
VStack(alignment: .leading){
HStack{
Text("What You Need")
.font(.system(.largeTitle, design: .serif))
.bold()
Spacer()
Button(action: {
self.showModal.toggle()
}) {
Image(systemName: "person.crop.circle.fill")
.font(.largeTitle)
.foregroundColor(.primary)
}
.sheet(isPresented: $showModal){
SettingView(showModal: self.$showModal)
}
}
Text("\(date, formatter : MainView.self.formatter)")
.font(.callout)
.bold()
.foregroundColor(Color(UIColor.systemGray4))
}
/*
Rectangle()
.frame(height: 2)
.cornerRadius(1)
.foregroundColor(Color(UIColor.systemGray5))
*/
}
.padding(.horizontal)
.padding(.top, 20)
}.background(Color(UIColor.systemBackground)).frame(minWidth: 0, maxWidth: .infinity).frame(height: 75)
}
Spacer()
}
}
} else {
Authentification()
}
}
.onAppear(perform: getUser)
//}
.navigationBarTitle("Menu Principal")
.navigationBarHidden(true)
}.navigationViewStyle(StackNavigationViewStyle())
}
//MARK: Functions
func getUser() {
session.listen()
}
}
and this the code of my detail view :
import SwiftUI
struct RedactorView: View {
var body: some View {
// NavigationView {
ScrollView {
VStack(alignment: .leading) {
HStack{
Image("P1")
.resizable()
.scaledToFill()
.frame(width: 115, height: 115)
.clipShape(Circle())
.padding(.trailing)
VStack(alignment: .leading){
Text("Prénom")
.font(.largeTitle)
.bold()
Text("Nom")
.font(.largeTitle)
.bold()
HStack {
VStack(alignment: .center){
Text("10")
.bold()
Text("Articles")
.foregroundColor(Color(UIColor.secondaryLabel))
}
VStack(alignment: .center){
Text("5")
.bold()
Text("Medias")
.foregroundColor(Color(UIColor.secondaryLabel))
}
}.padding(.top)
}
Spacer()
}.padding(.horizontal)
ScrollView(.horizontal, showsIndicators: false) {
HStack {
HStack{
Text("Email")
.bold()
Image(systemName: "envelope.fill")
// .foregroundColor(Color(UIColor.systemBlue))
}.foregroundColor(Color(UIColor.systemBlue))
.padding(8)
.background(Color(UIColor.systemBackground))
.clipShape(Capsule())
.shadow(color: Color(UIColor.systemGray6), radius: 10)
HStack{
Text("Instagram")
.bold()
Image(systemName: "heart.fill")
}.foregroundColor(Color(hue: 0.93, saturation: 0.81, brightness: 0.84)).padding(8)
.background(Color(UIColor.systemBackground))
.clipShape(Capsule())
.shadow(color: Color(UIColor.systemGray6), radius: 10)
HStack{
Text("Message")
.bold()
Image(systemName: "message.fill")
}.foregroundColor(Color(UIColor.systemGreen))
.padding(8)
.background(Color(UIColor.systemBackground))
.clipShape(Capsule())
.shadow(color: Color(UIColor.systemGray6), radius: 10)
HStack{
Text("Téléphone")
.bold()
Image(systemName: "phone.fill")
}.foregroundColor(Color(UIColor.systemGreen)).padding(8)
.background(Color(UIColor.systemBackground))
.clipShape(Capsule())
.shadow(color: Color(UIColor.systemGray6), radius: 10)
}.padding()
}
VStack(alignment: .leading, spacing: 5) {
Text("Ses Medias :")
.font(.largeTitle)
.bold()
.padding(.leading)
ScrollView(.horizontal, showsIndicators: false){
HStack{
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
MediaListRow()
.frame(width: UIScreen.main.bounds.width/1.15)
.scaledToFit()
}
}
Text("Ses Articles :")
.font(.largeTitle)
.bold()
.padding(.leading)
ArticleListRow()
ArticleListRow()
ArticleListRow()
}
}.navigationBarTitle("Redactor")
}
// }//.padding(.top, -20)
}
}
I don't know what is the solution of this I hope it open normal but it don't find and I have the same with all scroll view. Please help me.
it is missing a .edgesIgnoringSafeArea(.top) on the navigationView which can give you the safeArea guard.

Resources