Post

Replies

Boosts

Views

Activity

SwiftUI - View disappears if animated
I have asked this question on stack overflow. You may read it there, because I have included 2 GIFs that illustrates the problem. I am building a custom segmented control. This is the code that I have written. struct SegmentedControl: View { 		private var items: [String] = ["One", "Two", "Three"] 		 @Namespace var animation:Namespace.ID @State var selected: String = "One" var body: some View { ScrollView(.horizontal) { HStack { ForEach(items, id: \.self) { item in Button(action: { withAnimation(.spring()){ self.selected = item } }) { Text(item) .font(Font.subheadline.weight(.medium)) .foregroundColor(selected == item ? .white : .accentColor) .padding(.horizontal, 25) .padding(.vertical, 10) .background(zStack(item: item)) } } } .padding() } } private func zStack(item: String) -> some View { ZStack{ if selected == item { Color.accentColor .clipShape(Capsule()) .matchedGeometryEffect(id: "Tab", in: animation) } else { Color(.gray) .clipShape(Capsule()) }} } } A control is Blue when it is selected. However, sometimes if you navigate back and forth very fast, the Color.accentColor disappears Info, It is easier to test it on a physical device rather than a simulator.
0
0
1.1k
Dec ’20
SwiftUI - Navigate from UITableView (UIViewRepresentable) to View
I have a NavigationView that contains a UItableView at the bottom. At the top, there are some more views. I am using UItableView because I need both of its trailing and leading swipe actions. Since the table view is a subview of NavigationView, is there any way to access it in UITableView, in its didSelectRowAt method?&#9;&#9;func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { &#9;&#9;&#9;&#9;let destination = Text("Destination") &#9;&#9;&#9;&#9;let host = UIHostingController(rootView: destination) /*TODO: - Navigate to Destination *navigationController.pushViewController(host, animated: true)/ } UITableView struct TableView: UIViewRepresentable { &#9;&#9;@State var rows = ["London", "Paris", "Oslo"] &#9;&#9; func makeUIView(context: Context) -> UITableView { let table = UITableView() table.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") table.dataSource = context.coordinator table.delegate = context.coordinator return table } func updateUIView( uiView: UITableView, context: Context) { } func makeCoordinator() -> Coordinator { return Coordinator(rows: $rows) } class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate { @Binding var rows: [String] init(rows: Binding<[String]>) { self.rows = rows } func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.rows.count } func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") cell?.textLabel?.text = self.rows[indexPath.row] return cell! } func tableView( tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let destination = Text("Destination") let host = UIHostingController(rootView: destination) } func tableView( tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let add = UIContextualAction(style: .normal,title: "Add") { (action, view, success) in success(true) } add.backgroundColor = .gray return UISwipeActionsConfiguration(actions: [add]) } func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let remove = UIContextualAction(style: .normal,title: "Remove") { (action, view, success) in success(true) } remove.backgroundColor = .red let edit = UIContextualAction(style: .normal,title: "Edit") { (action, view, success) in success(true) } edit.backgroundColor = .gray let color = UIContextualAction(style: .normal, title: "Color") { (action, view, success) in success(true) } return UISwipeActionsConfiguration(actions: [remove, edit, color]) } } } View that contains the tableView struct ContentView: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView { &#9;&#9;&#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Some other Views") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TableView() &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} }
0
0
1.4k
Nov ’20
SwiftUI - ScrollViewReader's scrollTo does not scroll
I have a simple SwiftUI list that I want to scroll to a row when a user click on a button. This is my code that it suppose to work, but it does not do. struct ContentView: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;ScrollViewReader { proxy in &#9;&#9;&#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Button("Jump to #50") { proxy.scrollTo(5, anchor: .top) } List(0..<100) { i in Text("Example \(i)") .id(i) } } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } I tested it on iOS 14.2 both on simulator and a physical device. I read srollTo's documentation - https://developer.apple.com/documentation/swiftui/scrollviewproxy/scrollto(_:anchor:) but there is not much info. So how to scroll to a row, for example, row 50?
2
0
3.6k
Nov ’20
CoreData: warning: CoreData+CloudKit
I use NSPersistentCloudKitContainer. It seems that it the data synced in iCloud after a few seconds. However, I get the following warnings in the console. I do not understand what is it for. CoreData: warning: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(813): &lt;NSCloudKitMirroringDelegate: 0x280b051e0&gt;: Successfully set up CloudKit integration for store: &lt;NSSQLCore: 0x280024a00&gt; (URL: file:///private/var/mobile/Containers/Shared/AppGroup/89D9D17C-03DF-48E4-83C3-DB23A8FE0D6F/TestData.sqlite) CoreData: warning: CoreData+CloudKit: -[NSCloudKitMirroringDelegate checkAndScheduleImportIfNecessary:]_block_invoke(2033): &lt;NSCloudKitMirroringDelegate: 0x280b051e0&gt;: Scheduling automated import with activity: <CKSchedulerActivity: 0x281f51590; additionalXPCActivityCriteria={     Priority = Utility; }, containerID=&lt;CKContainerID: 0x283c612e0; containerIdentifier=iCloud.io.test.testData, containerEnvironment="Sandbox"&gt;, identifier=com.apple.coredata.cloudkit.activity.import, priority=2> Could you please explain what is this warning for and what should I do to fix it?
2
0
1.7k
Jul ’20