Post

Replies

Boosts

Views

Activity

when click list item, open new SwiftUI for every list items in SwiftUI
I have a simple SwiftUI project, when I click the any list items, I want to route different SwiftUI. Below, I used arrays, and for every SwiftUI, I want to use [0], [1],..., but it throw error, I do not know why? Any idea? ContentView: import SwiftUI struct ContentView: View {   var body: some View {     NavigationView {       List(contacts) { contact in         NavigationLink(destination: NumberOneView(contact: contact)) {           ContactRow(contact: contact)         }       }       .navigationBarTitle("Contacts")     }     .environment(\.colorScheme, .light)   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } struct ContactRow: View {       let contact: Settings       var body: some View {     HStack {       Image(contact.imageName)         .resizable()         .aspectRatio(contentMode: .fill)         .frame(width: 20, height: 20)                          VStack(alignment: .leading) {         Text(contact.name)           .font(.system(size: 21, weight: .medium, design: .default))                }     }   } } Settings.swift: import Foundation import SwiftUI struct Settings: Identifiable { let imageName: String let name: String let id = UUID() } let contacts = [ Settings(imageName: "image1", name: "NumberOne"), Settings(imageName: "image2", name: "NumberTwo"), Settings(imageName: "image3", name: "NumberThree"), Settings(imageName: "image4", name: "NumberFour"), ] NumberOneView: import SwiftUI struct NumberOneView: View { let contact: Settings var body: some View { Text("hey") } } struct NumberOneView_Previews: PreviewProvider { static var previews: some View { NumberOneView(contact: contacts[0]) } } NumberTwoView: import SwiftUI struct NumberTwoView: View { let contact: Settings var body: some View { Text("hey") } } struct NumberTwoView_Previews: PreviewProvider { static var previews: some View { NumberTwoView(contact: contacts[1]) } }
6
0
4.2k
Mar ’21
How can I handle NavigationLink for HStack row
I want to use NavigationLink for open the chat detail view when I click the rows items. Here example of code I mentioned. Any idea will be appreciated. RecentRowView: import SwiftUI struct RecentRowView: View { var recent: Profile var animation: Namespace.ID // Environment Object... @EnvironmentObject var profileData: ProfileDetailModel var body: some View { HStack(spacing: 15){ // Making it as clickable Button.... Button(action: { withAnimation{ profileData.selectedProfile = recent profileData.showProfile.toggle() } }, label: { ZStack{ // Without matched geometry effect simply showing image... Image(recent.profile) .resizable() .aspectRatio(contentMode: .fill) .frame(width: 60, height: 60) .clipShape(Circle()) if !profileData.showProfile{ Image(recent.profile) .resizable() .aspectRatio(contentMode: .fill) // Matched Geometry Effect... // Giving unique ID that is from UUID from profile Model.... .matchedGeometryEffect(id: recent.id, in: animation) .frame(width: 60, height: 60) .clipShape(Circle()) } } }) // it decreased the highlight color.... .buttonStyle(PlainButtonStyle()) VStack{   NavigationLink(destination: ChatDetailView(), isActive: $profileData.show) { HStack{ VStack(alignment: .leading, spacing: 8, content: { Text(recent.userName) .fontWeight(.bold) Text(recent.lastMsg) .font(.caption) .foregroundColor(.gray) }) Spacer(minLength: 10) Text(recent.time) .font(.caption2) .foregroundColor(.gray) } Divider() } } } .padding(.horizontal) } } struct RecentRowView_Previews: PreviewProvider { static var previews: some View { ContentView() } } ContentView: struct ContentView: View { // ANimation Namespace... @Namespace var animation // StateObject... @StateObject var profileData = ProfileDetailModel() var body: some View { Home(animation: animation) // setting Environment Object... .environmentObject(profileData) } } struct ContentView_Previews: PreviewProvider { static var previews: some View {     NavigationView{ ContentView() } } } ChatDetailView: import SwiftUI struct ChatDetailView: View {   var body: some View {     Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)   } } struct ChatDetailView_Previews: PreviewProvider {   static var previews: some View {     ChatDetailView()   } }
6
0
2.1k
Jun ’21
Click circle image and navigate new SwiftUI
I have custom search bar and custom circle image in toolbar, when I click the circle image, I want to navigate new SwiftUI. Any idea will be appreciated. ImageView: VStack(alignment: .leading, spacing:0){              HStack(spacing: 0){                 TextField("Search", text: $search)           .padding(.vertical,5)           .padding(.horizontal)           .background(Color.gray.opacity(0.090))           .cornerRadius(30)           .frame(width: 330, height: 32)                  Image("imgage")           .resizable()           .aspectRatio(contentMode: .fill)           .frame(width: 32, height: 32)           .clipShape(Circle())           .overlay(Circle().stroke(Color("fig"), lineWidth: 1))               } } ContentView: var body: some View {    //I have tabview here// }
9
0
2.7k
Mar ’21
Trailing closure passed to parameter of type 'Int' that does not accept a closure
I have TabView in ContentView and I want to add TabView for OnboardingView in OtherView, every things work, but it is throw error for TabView in OtherView like "Trailing closure passed to parameter of type 'Int' that does not accept a closure" I do not know why? Any idea? ContentView: struct TabView : View {       var body: some View{           VStack(spacing: 0){ ....... } OtherView:    VStack {     TabView {       ForEach(onboardingData) { onboardingItem in          OnboardingCard(onboardingItem: onboardingItem)       }   }   .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))   .indexViewStyle(PageIndexViewStyle (backgroundDisplayMode:   .always))   .foregroundColor(.white) }
9
1
6.7k
5d
Thread 1: Fatal error: No ObservableObject of type msgDatas found. A View.environmentObject(_:) for msgDatas may be missing as an ancestor of this view.
It throw fatal error for data in "  self.data.selectedData = i", even I try to calling @State for solve, but nothing change. Main: struct Main : View {       @EnvironmentObject var data : msgDatas   @State var show: Bool = false       var body : some View{           List(msgs){i in               cellView(pic: i.pic, name: i.name, msg: i.msg, time: i.time, msgs: i.msgs).onTapGesture {                   self.data.selectedData = i         self.show.toggle()       }     }   } } cellView: struct cellView : View {       var pic : String   var name : String   var msg : String   var time : String   var msgs : String       var body : some View{           HStack(spacing: 15){               Image(pic).resizable().frame(width: 50, height: 50).clipShape(Circle())               VStack(alignment:.leading,spacing: 5){                   Text(name)         Text(msg).lineLimit(2)       }               Spacer()               VStack(spacing: 10){                   Text(time)         if msgs != ""{                       Text(msgs).padding(8).background(Color("bg")).foregroundColor(.white).clipShape(Circle())         }         else{                       Spacer()         }       }             }.padding(9)   } }
30
0
3.6k
Mar ’21