how to make 'onTapGesture' start another view - swift

I have a NavigationView with a card of Info and then a list, I would like to tap the info and go to another view, or you could tap an item of a list and see more info about an item.
So, if I give 'onTapGesture' modifier to the card view it does print"onTap pressed', but I can't figure our how to make it start another view
var body: some View {
NavigationView {
VStack {
TopResultsCard(games: self.games)
.padding(.top, 10)
.onTapGesture({
print("onTap tapped")
})
ResultsListItem(games: self.games)
}.navigationBarTitle("", displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {
self.showNewResult.toggle()
}) {
Image(systemName: "plus.circle")
Text("Add result")
}
.sheet(isPresented: $showNewResult) {AddResultView(games: self.games)}
.font(.caption))
}
}

Here is possible approach. Use your destination view instead of demo Text("Next view here") below.
#State private var isActive = false
var body: some View {
NavigationView {
VStack {
TopResultsCard(games: self.games)
.padding(.top, 10)
.onTapGesture { self.isActive.toggle() }
.background(NavigationLink(destination:
Text("Next view here"), isActive: $isActive) { EmptyView() })
ResultsListItem(games: self.games)
}.navigationBarTitle("", displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {
self.showNewResult.toggle()
}) {
Image(systemName: "plus.circle")
Text("Add result")
}
.sheet(isPresented: $showNewResult) {AddResultView(games: self.games)}
.font(.caption))
}
}

Related

.ignoresSafeArea(.keyboard) does not work on SwiftUI view

I have the following view which is presented as a sheet:
var body: some View {
NavigationView {
GeometryReader { reader in
VStack {
TransactionAmount(amountString: $amountString)
.padding(.top, Constants.amountPadding)
Spacer()
SelectedCategory(selectedCategory: $selectedCategory)
.padding()
.onTapGesture {
isCategoryPickerPresented = true
UIImpactFeedbackGenerator.feedback(style: .medium)
}
AddTransactionToolbar(noteText: $noteText)
.padding(.horizontal)
NumberPad(numberString: $amountString, selectedCategory: $selectedCategory)
}
.sheet(isPresented: $isCategoryPickerPresented) {
CategoryPicker(selection: $selectedCategory)
.presentationDetents([.medium])
}
.onAppear {
selectedCategory = storageManager.categories.first
}
.ignoresSafeArea(.keyboard, edges: .bottom)
.ignoresSafeArea(.keyboard)
.navigationTitle("Add Transaction")
.navigationBarTitleDisplayMode(.inline)
}
}
}
For some reason ignoresSafeArea(.keyboard) is not working and my view is being pushed up when the keyboard is presented. Wrapping the view in GeometryReader hasn't made any difference either. Not really sure where to go from here. How do I fix this?

Swiftui invisible bar on the top of the screen

I have the following in problem.
If I try to give a view a color (Color.red for example)
I get the following output:
I can just add .edgesignoressafearea(.top) and the top also gets red. But when I want to add an clickable item, the user won't be able to click it since there still is this invisible bar on the top of the screen. Does Anyone know what my problem is? The problem is in all the tabable views(Timeline, Favorits, Discover, Account). So It must be in either the first code or in tabview (second code) that I send in this post.
When the user clicks on the app first they get this view sending the user to the signin view or the app itself:
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: tabView().navigationBarHidden(true), isActive: $tabview, label: { EmptyView() })
NavigationLink(destination: loginView().navigationBarHidden(true), isActive: $login, label: { EmptyView() })
if tabview == false && login == false {
Text("loading")
.onAppear(perform: checklogin)
}
}
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
}
}
Then the apps sends them to tabview:
var body: some View {
TabView(selection: $selection) {
Timeline()
.tabItem {
Label("timeline", systemImage: "house")
}
.tag(0)
Favorits()
.tabItem {
Label("favorits", systemImage: "heart")
}
.tag(1)
Discover()
.tabItem {
Label("discover", systemImage: "network")
}
.tag(2)
Account()
.tabItem {
Label("account", systemImage: "person")
}
.tag(3)
}
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
}
The problem happens on all of this views.
This is the view where I made the screenshot of:
var body: some View {
ZStack {
Color.red
Text("Hello fav!")
}
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
}
add .edgesIgnoringSafeArea(.top) and remove your navigation bar if you don't want to be able to get back to your login view anytime.
That's the full code with a login page, now it depends what you want on that login page :
import SwiftUI
struct ContentView: View {
#State var showLoginView: Bool = false
var body: some View {
VStack {
if showLoginView {
MainView()
} else {
Button("Login") {
self.showLoginView = true
}
}
}
}
}
struct MainView: View {
var body: some View {
TabView {
TimeLine()
.tabItem {
Label("timeline", systemImage: "house")
}
Favorits()
.tabItem {
Label("favorits", systemImage: "heart")
}
Discover()
.tabItem {
Label("discover", systemImage: "network")
}
Account()
.tabItem {
Label("account", systemImage: "person")
}
}
}
}
struct TimeLine: View {
var body: some View {
Color.blue
.edgesIgnoringSafeArea(.top)
}
}
struct Favorits: View {
var body: some View {
ZStack {
Color.red
.edgesIgnoringSafeArea(.top)
Text("Hello fav!")
}
}
}
struct Discover: View {
var body: some View {
Color.yellow
.edgesIgnoringSafeArea(.top)
}
}
struct Account: View {
var body: some View {
Color.purple
.edgesIgnoringSafeArea(.top)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You need to set the navigation Title to "" because your View is embedded in NavigationView
Create a ViewModifier like this and apply it to your VStack
struct HiddenNavBarModifier: ViewModifier {
func body(content: Content) -> some View {
content
.navigationBarTitle("", displayMode: .inline)
.navigationBarHidden(true)
}
}

SwiftUI Set Image Boundary

i want to set my right image frame boundaries like the left one without moving image or text. How can I do that. I tried a couple way but I couldn't find the solution.
You should use the built-in TabView. It provides all the functionality for you, and already made with accessibility in mind.
Here is an example (you can change it for your text and images):
struct ContentView: View {
#State private var selection: Int = 1
var body: some View {
TabView(selection: $selection) {
Text("Tab Content 1")
.tabItem {
Label("1", systemImage: "1.square")
}
.tag(1)
Text("Tab Content 2")
.tabItem {
Label("2", systemImage: "2.square")
}
.tag(2)
Text("Tab Content 3")
.tabItem {
Label("3", systemImage: "3.square")
}
.tag(3)
}
}
}
Result:
Custom version (highly unrecommended):
struct ContentView: View {
var body: some View {
VStack {
Spacer()
Text("Main Content")
Spacer()
HStack {
VStack {
Button {
//
} label: {
Label("1", systemImage: "1.square")
}
}.frame(maxWidth: .infinity)
VStack {
Button {
//
} label: {
Label("2", systemImage: "2.square")
}
}.frame(maxWidth: .infinity)
VStack {
Button {
//
} label: {
Label("3", systemImage: "3.square")
}
}.frame(maxWidth: .infinity)
}.frame(height: 50)
}
}
}

Custom action sheet with SwiftUI

I have a TabView at the top of navigation and a tab which contains the user profile.
I want to present an action sheet with custom style when users touch cover/profile picture.
The custom style looks like this:
I already try to attach a second view at the bottom of the ZStack but the TabView always looks in front.
How can I custom the action sheet or how can I hide TabView?
Thanks
ZStack approach will work in this scenario.
I have tried with a small example, you can tweak and try to fit in your project and see if it works.
Implementation :
struct ContentView: View {
#State var showingCustomSheet = false
var body: some View {
ZStack {
VStack {
Button(action: {
withAnimation {
self.showingCustomSheet.toggle()
}
}) {
Text("Show Custom sheet")
}
}
ZStack {
HStack(alignment: .bottom) {
Spacer()
VStack (alignment: .leading, spacing: 10) {
Spacer()
Button(action: {}) { Text("Delete photo") }
Button(action: {}) { Text("Take photo") }
Button(action: {}) { Text("Choose photo") }
Button(action: {
withAnimation {
self.showingCustomSheet.toggle()
}
}) { Text("Cancel") }
}
Spacer()
}
}.background(Color.black.opacity(0.1))
.edgesIgnoringSafeArea(.all)
.offset(x:0, y: self.showingCustomSheet ? 0 : UIApplication.shared.keyWindow?.frame.height ?? 0)
}
}
}

another view with action button in navigation Bar Items

I want to open a new view using a button that is in a navigationBarItems. I put my code. Thanks for your help.
struct ClienteView: View {
var body: some View {
NavigationView {
List(clientes) { cliente in
NavigationLink(destination: DetallesClienteView(objCliente: cliente)){
DetallesCliente(objCliente: cliente)
}
}
.navigationBarTitle(Text("Clientes"), displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {
}) {
Image(systemName: "person.badge.plus")
}
)
}
}
}
You can do this kind of thing:
.navigationBarItems(trailing:
NavigationLink(destination: SecondView(), label: {
Image(systemName: "person.badge.plus")
})
)