swiftui

SwiftUI memory leak when referencing property from closure inside Form/NavigationView and swapping views

给你一囗甜甜゛ 提交于 2020-12-29 12:56:05
问题 I have something like this: struct SomeView: View { @ObservedObject var viewModel: SomeViewModel var body: some View { NavigationView { // <- culprit Button(action: { self.viewModel.logOut() }) { Text("X").frame(width: 40, height: 40) } } } class SomeViewModel: ObservableObject { func logOut() { // changes global state, based on which the views are swapped, so `SomeView` is removed and replaced by a different one } } When the button is pressed, SomeView is closed and a different view is

SwiftUI .toolbar disappears after following NavigationLink and coming back

纵然是瞬间 提交于 2020-12-29 12:27:28
问题 I've added a .toolbar to the top level of a NavigationView that will eventually be used to select items in a list without using swipe gestures (up button, down button, etc.). I also have a .navigationBar going on, to access other views for Account and Settings. For the most part it's looking really good, but when I follow a NavigationLink (in .navigationBarItems) within NavigationView, and then use the built-in back navigation , my .toolbar disappears from the top level. Am I putting the

Timer onReceive not working inside NavigationView

一曲冷凌霜 提交于 2020-12-29 07:41:11
问题 I have the following timer: struct ContentView: View { @State var timeRemaining = 10 let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var body: some View { NavigationView{ VStack { if(self.timeRemaining > 0) { Text("\(timeRemaining)") .onReceive(timer) { _ in if self.timeRemaining > 0 { self.timeRemaining -= 1 } } } else { Text("Time is up!") } } } } } If I remove the NavigationView view, the timer updates and works, but like that it doesn't, what's going on here and

Timer onReceive not working inside NavigationView

允我心安 提交于 2020-12-29 07:41:09
问题 I have the following timer: struct ContentView: View { @State var timeRemaining = 10 let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var body: some View { NavigationView{ VStack { if(self.timeRemaining > 0) { Text("\(timeRemaining)") .onReceive(timer) { _ in if self.timeRemaining > 0 { self.timeRemaining -= 1 } } } else { Text("Time is up!") } } } } } If I remove the NavigationView view, the timer updates and works, but like that it doesn't, what's going on here and

SwiftUI NavigationView navigationBarTitle LayoutConstraints issue

梦想与她 提交于 2020-12-29 05:55:58
问题 This is maybe related to the new (12.3) version of XCode that came out recently but I have a very simple SwiftUI View: import SwiftUI struct HomeView: View { var body: some View { NavigationView { Text("Text") .navigationBarTitle("My Title") } } } And I'm getting this warning in the console: 2020-12-15 18:25:06.506062-0800 Shopmatic[46177:9585655] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't

How to get local tap position in onTapGesture?

牧云@^-^@ 提交于 2020-12-29 04:33:48
问题 Have some rendered image and need to determine tap position relative to image. Seems to be simple, but can't find the answer var body: some View { VStack { Image(uiImage: drawRectangle()) // some rendered image .onTapGesture { // print(x, y)? } 回答1: Found this solution: .gesture( DragGesture(minimumDistance: 0, coordinateSpace: .global) .onChanged { value in self.position = value.location } .onEnded { _ in self.position = .zero } ) setting minimumDistance: 0 will call onChanged immediately

SwiftUI - ObservableObject created multiple times

爷,独闯天下 提交于 2020-12-28 20:39:39
问题 I have created an ObservableObject in a View. @ObservedObject var selectionModel = FilterSelectionModel() I put a breakpoint inside the FilterSelectionModel 's init function and it is called multiple times. Because this View is part of a NavigationLink , I understand that it gets created then and along with it, the selectionModel. When I navigate to the View, the selectionModel is created again. In this same View I have a "sub View" where I pass the selectionModel as an EnvironmentObject so

Change Tabbed View Bar Color SwiftUI

耗尽温柔 提交于 2020-12-28 18:37:21
问题 Does anyone know how to change the background colour of a tabbed view bottom bar? I have set the accent colour which changed the colour of my icons when I select each tab bar item. I have tried setting the background to a colour but it doesn't change the back, and tried setting background to an image just to be sure but that also doesn't do anything. Wondering if I need to specifically access the bottom bar somehow and then set a property on that? 回答1: Here is a solution. You can change

Change Tabbed View Bar Color SwiftUI

a 夏天 提交于 2020-12-28 18:33:46
问题 Does anyone know how to change the background colour of a tabbed view bottom bar? I have set the accent colour which changed the colour of my icons when I select each tab bar item. I have tried setting the background to a colour but it doesn't change the back, and tried setting background to an image just to be sure but that also doesn't do anything. Wondering if I need to specifically access the bottom bar somehow and then set a property on that? 回答1: Here is a solution. You can change

Xcode 11 beta swift ui preview not showing

半腔热情 提交于 2020-12-28 04:32:03
问题 Just playing with Swift UI basic app and the preview canvas is not showing even though I'm in canvas mode. App runs, and I have this little snippet what am I missing? #if DEBUG struct ContentView_Previews : PreviewProvider { static var previews: some View { ContentView() } } #endif 回答1: To preview and interact with views from the canvas in Xcode, ensure your Mac is running on Catalina MacOS . https://developer.apple.com/tutorials/swiftui/creating-and-combining-views Please check apple