swiftui

How to make a button draggable/movable with SwiftUI?

狂风中的少年 提交于 2020-12-12 15:52:12
问题 I am trying to make a Button Movable with SwiftUI. From what it looks like this should work. I have tried putting the Button with Text inside another ZStack and for a second it was working but as soon as I released the button, the dragging stopped and I couldn't drag anymore. I noticed that the tap was remaining in the center despite the button had moved. Also the dragging looked buggy. struct CircleButton: View { @State private var dragAmount = CGSize.zero var body: some View { ZStack {

How to figure out the width of a string dynamically in SwiftUI

こ雲淡風輕ζ 提交于 2020-12-12 11:14:58
问题 Let's say I have a text called: This . I want to figure out the width of the text called This so that I can assign a width to another view's frame. How do I go about it? struct BadgeTextView: View { var text: String var body: some View { Rectangle() .fill(Color.red) .cornerRadius(3) .frame(width: text.???, height: <#T##CGFloat?#>, alignment: <#T##Alignment#>) } } 回答1: This extension to String should build on Sweeper's suggestion to let you get the width of a String given a certain font :

StoreKit Delegate / Observable object in SwiftUI View

浪尽此生 提交于 2020-12-12 06:28:29
问题 When I restored my purchases, I need to disabled the restore button in the view. I have IAPManager class with SKPaymentTransactionObserver , and it works fine, I saw the print("restored") of SKPaymentTransactionObserver when I call SKPaymentQueue.default().restoreCompletedTransactions from my principal view. So, then the problem is that from the view I do not find out about that event and therefore I cannot disable the button, since the purchase has already been restored. In my IAPManager:

StoreKit Delegate / Observable object in SwiftUI View

匆匆过客 提交于 2020-12-12 06:26:04
问题 When I restored my purchases, I need to disabled the restore button in the view. I have IAPManager class with SKPaymentTransactionObserver , and it works fine, I saw the print("restored") of SKPaymentTransactionObserver when I call SKPaymentQueue.default().restoreCompletedTransactions from my principal view. So, then the problem is that from the view I do not find out about that event and therefore I cannot disable the button, since the purchase has already been restored. In my IAPManager:

Accessing AppState in AppDelegate with SwiftUI's new iOS 14 life cycle

孤者浪人 提交于 2020-12-12 05:12:32
问题 I'm using SwiftUI's new app lifecycle coming in iOS 14. However, I'm stuck at how to access my AppState (single source of truth) object in the AppDelegate . I need the AppDelegate to run code on startup and register for notifications ( didFinishLaunchingWithOptions , didRegisterForRemoteNotificationsWithDeviceToken , didReceiveRemoteNotification ) etc. I am aware of @UIApplicationDelegateAdaptor but then I can not e.g. pass an object through to the AppDelegate with a constructor. I guess the

SwiftUI NavigationView not see Image

这一生的挚爱 提交于 2020-12-12 04:04:33
问题 I have a code and make NavigationLink Button, I write Text and Image, But my Image not a see. Pls help me. VStack{ Image("Coachs") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 370, height: 200) //.clipped() .cornerRadius(20.0) NavigationLink(destination: Text("Detail view here")){ ZStack{ Text("Press on me") .foregroundColor(.white) .font(.largeTitle) Image("Coachs") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 370, height: 200) //.clipped() .cornerRadius(20.0) }

SwiftUI | Stop TextField from moving up with keyboard automatically [duplicate]

落爺英雄遲暮 提交于 2020-12-12 01:33:24
问题 This question already has answers here : iOS 14 SwiftUI Keyboard lifts view automatically (3 answers) Closed 2 months ago . Since iOS14 I have found that all my TextField s are moving up automatically once the keyboard appears. I haven't found a way to stop this, have I missed something? Here is a simple Demo+Code: Demo: Code: import SwiftUI struct ContentView: View { @State private var textInput: String = "" var body: some View { TextField("Test", text: $textInput) } } 回答1: Here is possible

How can I programmatically edit TextField's border color in SwiftUI?

六眼飞鱼酱① 提交于 2020-12-11 00:48:57
问题 Here's the code snippet: TextField("Email", text: self.$email) .padding() .overlay(RoundedRectangle(cornerRadius: 1) .stroke(Color.black, lineWidth: 1)) SecureField("Password", text: self.$password) .padding() .overlay(RoundedRectangle(cornerRadius: 1) .stroke(Color.black, lineWidth: 1)) Button(action: { print("The button was clicked!") if loginAndPasswordAreOK() { print("Login & Password are OK!") } else { self.email = "" self.password = "" } }, label: { Text("Log In") .fontWeight(.bold)

How can I programmatically edit TextField's border color in SwiftUI?

杀马特。学长 韩版系。学妹 提交于 2020-12-11 00:47:32
问题 Here's the code snippet: TextField("Email", text: self.$email) .padding() .overlay(RoundedRectangle(cornerRadius: 1) .stroke(Color.black, lineWidth: 1)) SecureField("Password", text: self.$password) .padding() .overlay(RoundedRectangle(cornerRadius: 1) .stroke(Color.black, lineWidth: 1)) Button(action: { print("The button was clicked!") if loginAndPasswordAreOK() { print("Login & Password are OK!") } else { self.email = "" self.password = "" } }, label: { Text("Log In") .fontWeight(.bold)

How can I programmatically edit TextField's border color in SwiftUI?

安稳与你 提交于 2020-12-11 00:47:10
问题 Here's the code snippet: TextField("Email", text: self.$email) .padding() .overlay(RoundedRectangle(cornerRadius: 1) .stroke(Color.black, lineWidth: 1)) SecureField("Password", text: self.$password) .padding() .overlay(RoundedRectangle(cornerRadius: 1) .stroke(Color.black, lineWidth: 1)) Button(action: { print("The button was clicked!") if loginAndPasswordAreOK() { print("Login & Password are OK!") } else { self.email = "" self.password = "" } }, label: { Text("Log In") .fontWeight(.bold)