Post

Replies

Boosts

Views

Activity

`disabled` does not wok in List selection
disabled does not wok in List selection struct ListDisableTest: View {   let disableIDs: [Int] = [1, 3]   @State var selection: Set<Int> = []   var body: some View {     List(1..<20, id: \.self, selection: $selection) { i in       Text("\(i)")         .tag(i)         .disabled(disableIDs.contains(i))     }     .environment(\.editMode, .constant(.active))   } }
1
0
482
Jan ’23
SwiftUI's @State reuses same data?
SwiftUI's @State reuses same data? (ContentView) switch user then, (SubView) SubView should reset dates([Date]), But dates has previous data. struct ContentView: View {   let users: [String]   @State var selectedUser: String   init() {     self.users = ["User1", "User2", "User3"]     self.selectedUser = users.first!   }   var body: some View {     VStack {       Text(selectedUser)       Picker("User", selection: $selectedUser) {         ForEach(users, id: \.self) { user in           Text(user)             .tag(user)         }       }       .pickerStyle(.segmented)       SubView(user: selectedUser)     }   } } struct SubView: View {   let user: String   @State var dates: [Date] = []   var body: some View {     List {       Section(user) {         Button("Add") {           dates.append(.now)         }         ForEach(dates, id: \.self) { date in           Text(date, format: .iso8601)         }       }     }   } }
1
0
733
Dec ’22
Regex Error Thread 1: EXC_BREAKPOINT (code=1, subcode=0x22a78b848)
I run into Thread 1: EXC_BREAKPOINT Error, This code should be right. import Foundation import RegexBuilder let string = "https://swift.org, #swift, @swift" let regex = Regex {   ChoiceOf {     OneOrMore(.url())     WordWithPrefix(prefix: "@")     WordWithPrefix(prefix: "#")   } } struct WordWithPrefix: RegexComponent {   let prefix: String   @RegexComponentBuilder   var regex: Regex<some RegexComponent> {     prefix     OneOrMore(.word)   } } for match in string.matches(of: regex) { // Thread 1: EXC_BREAKPOINT (code=1, subcode=0x22a78b848)   print(String(string[match.range])) }
1
0
1.2k
Aug ’22
[SwiftUI]Binding, dismiss Bug?
back with dismiss(Environment) and view reappears. struct Person: Identifiable {   let id = UUID()   let name: String } struct ContentView: View {   @State var people: [Person] = [.init(name: "mike")]   @State var isPresented = false   var body: some View {     NavigationStack {       List(people) { person in         Text(person.name)       }       .navigationTitle("People")       .navigationDestination(isPresented: $isPresented) {         NewPerson(people: $people)       }       .toolbar {         ToolbarItem {           Button("Add") {             isPresented.toggle()           }         }       }     }   } } struct NewPerson: View {   @Environment(\.dismiss) var dismiss   @Binding var people: [Person]   var body: some View {     Button("Add and Back") {       people.append(.init(name: "ace"))       dismiss()     }   } }
2
0
815
Aug ’22
How to use Multi NavigationLink in One Cell of List?
NavigationLink in List is automatically designed. How to set appropriate multi NavigationLink in Cell? I don't want use ForEach in ScrollView. Because I want to use swipeActions. struct TimelineView: View {   let tweets: [Tweet] = tweets   let users: [User] = users   var body: some View {     NavigationStack {       List(tweets) { tweet in         let user = users.first { $0.id == tweet.userID }!         NavigationLink(value: tweet) {           HStack {             NavigationLink(value: user) {               Image(systemName: "person")                 .foregroundStyle(user.color)             }             VStack {               Text("@\(user.name)")               Text(tweet.text)             }           }         }         .swipeActions(edge: .leading) {           Button("Like") { }         }       }       .navigationDestination(for: Tweet.self) { tweet in         TweetDetailView(tweet: tweet)       }       .navigationDestination(for: User.self) { user in         UserDetailView(user: user)       }     }   } }
1
0
968
Jul ’22
ToolbarItemGroup(placement: .keyboard) is not showed with Sheet
struct ContentView: View {   @State var isPresented = false   var body: some View {     Button {       isPresented.toggle()     } label: {       Text("Button")     }     .sheet(isPresented: $isPresented) {       SubView()     }   } } struct SubView: View {   @State var text = ""   var body: some View {     NavigationStack {       TextEditor(text: $text)         .toolbar {           ToolbarItemGroup(placement: .bottomBar) {             Button("Click") {             }           }           ToolbarItemGroup(placement: .keyboard) {             Button("Click") {             }           }         }     }   } }
9
3
3.7k
Jun ’22
How to Active NavigationLink at Change Data in iOS 16
NavigationLink(destination:isActive:label:) is deprecated in iOS 16 How to implement below View in iOS 16 without Deprecated? struct ContentView: View {   @State var isPresentedDetailView = false   @State var isPresentedImageView = false   var body: some View {     NavigationStack {       List {         HStack {           Button("Name") { isPresentedDetailView.toggle() }           NavigationLink(isActive: $isPresentedDetailView) {             Text("Detail View")           } label: {             EmptyView()           }           .frame(width: 0, height: 0)           .hidden()           Image(systemName: "person")             .onTapGesture {               isPresentedImageView.toggle()             }           NavigationLink(isActive: $isPresentedImageView) {             Image(systemName: "person")           } label: {             EmptyView()           }           .frame(width: 0, height: 0)           .hidden()         }       }     }   } }
3
1
4.1k
Jun ’22
How load Video with New(iOS 16) PhotosPicker? loadTransferable(type:)
I use loadFileRepresentation(forTypeIdentifier:completionHandler:) to load video with PHPickerViewController What can I use load video with? // my sample code func loadPhoto(pickerItem: PhotosPickerItem) async throws -> Photo {     if let livePhoto = try await pickerItem.loadTransferable(type: PHLivePhoto.self) {       let photo: Photo = .init(id: pickerItem.itemIdentifier, item: livePhoto)       return photo     } else if let url = try await pickerItem.loadTransferable(type: URL.self) {       let photo: Photo = .init(id: pickerItem.itemIdentifier, item: url)       return photo     } else if let data = try await pickerItem.loadTransferable(type: Data.self) {       let photo: Photo = .init(id: pickerItem.itemIdentifier, item: data)       return photo     }     throw PhotoError.load   }
3
2
3.5k
Jun ’22