swiftui

SwiftUI @Binding update doesn't refresh view

删除回忆录丶 提交于 2021-02-18 10:15:05
问题 I feel like I'm missing something very basic, but this example SwiftUI code will not modify the view (despite the Binding updating) when the button is clicked Tutorials I have read suggest this is the correct way to use a binding and the view should refresh automatically import SwiftUI struct ContentView: View { @Binding var isSelected: Bool var body: some View { Button(action: { self.isSelected.toggle() }) { Text(isSelected ? "Selected" : "Not Selected") } } } struct ContentView_Previews:

SwiftUI @Binding update doesn't refresh view

有些话、适合烂在心里 提交于 2021-02-18 10:14:28
问题 I feel like I'm missing something very basic, but this example SwiftUI code will not modify the view (despite the Binding updating) when the button is clicked Tutorials I have read suggest this is the correct way to use a binding and the view should refresh automatically import SwiftUI struct ContentView: View { @Binding var isSelected: Bool var body: some View { Button(action: { self.isSelected.toggle() }) { Text(isSelected ? "Selected" : "Not Selected") } } } struct ContentView_Previews:

What’s the equivalent to String.localizedStringWithFormat(_:_:) for SwiftUI's LocalizedStringKey?

本秂侑毒 提交于 2021-02-18 05:24:25
问题 What’s the equivalent to String.localizedStringWithFormat(_:_:) in SwiftUI? I know LocalizedStringKey.init(:) can make use of string interpolation, but as I understand it this requires localizable string keys to be parameterized in the .strings/.stringsdict files. This is different to how localized string keys are currently defined in the app I'm working on. Given these localizable strings in Localizable.strings: "HELLO_WORLD" = "Hello, world!"; "HELLO_WORLD_PARAMETERIZED" = "Hello, %@!";

How to add placeholder text to TextEditor in SwiftUI?

半世苍凉 提交于 2021-02-18 05:07:09
问题 When using SwiftUI's new TextEditor, you can modify its content directly using a @State. However, I haven't see a way to add a placeholder text to it. Is it doable right now? I added an example that Apple used in their own translator app. Which appears to be a multiple lines text editor view that supports a placeholder text. 回答1: It is not possible out of the box but you can achieve this effect with ZStack or the .overla y property. What you should do is check the property holding your state.

How to apply shadow to interior views in SwiftUI?

大城市里の小女人 提交于 2021-02-17 18:51:34
问题 I have added a shadow around the VStack that holds my two text fields and a submit button. However, the shadow is also being applied to the two text fields inside the VStack . Is there something I am missing here that is causing this to happen? I tried adding shadow(radius: 0) on the text fields, but it doesn't change anything. If I remove the padding and background from the text fields, then the shadow goes away. var body: some View { VStack() { Spacer() VStack() { TextField($email,

Custom font size for Text in SwiftUI

我的未来我决定 提交于 2021-02-17 08:41:31
问题 I have a label in my view that I want to use the system font size in medium, with a size of 21 points. I created a custom extension to re-use the font created: extension Font { static var primaryButton: Font { return Font.custom("SFUIDisplay-Light", size: 21) } } However, this does not have any effect. I changed the string to HelveticaNeue-UltraLight and it did work, so I'm guessing that SFUIDisplay-Light is simply the incorrect font name. In font book, it says SFProText-Light , but that also

How to reset a subview in SwiftUI?

有些话、适合烂在心里 提交于 2021-02-17 07:11:23
问题 Below is a simplified version of the code that I'm using. But whenever I resetKeyboard() it still shows the previous keyboard. Is there anyway to make it so when I call resetKeyboard() it replaces the keyboard with a fresh KeyboardView? struct GameView: View { @State var myKeyboard = KeyboardView() var body: some View { VStack{ Button("Change Keyboard") { myKeyboard.change() } myKeyboard Button("Reset Keyboard") { resetKeyboard() } } } func resetKeyboard(){ self.myKeyboard = KeyboardView() }

How to disambiguate a foreach loop in Xcode with SwiftUI

梦想与她 提交于 2021-02-17 07:08:52
问题 I am trying to create a hexagon grid using swiftUI on Xcode. To loop over the creation of the individual hexagons I am using a foreach loop. However I came Across an error when creating a function that creates a column of hexagons: private func makeCol(C: Int, Bump:Int) -> some View { return ZStack { ForEach(-5...5, id: \.self) {Ynumber in self.hexagon( x: (ThreeRooted * self.L * Double(C)) + self.centerWidth, y: (ThreeRooted * self.L * Double(Ynumber)) + self.centerHeight +

SwiftUI: How can I detect if two views are intersecting each other?

与世无争的帅哥 提交于 2021-02-17 06:54:09
问题 One view is intersecting the other one. How could we detect this intersection in SwiftUI? In Swift I would reach this with a few lines of code: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let a = UIView(frame: CGRect(x: 100, y: 100, width: 150, height: 150)) a.backgroundColor = .purple view.addSubview(a) let b = UIView(frame: CGRect(x: 150, y: 150, width: 150, height: 150)) b.backgroundColor = .orange view.addSubview(b) if a.frame

What are all valid ways of writing closures that return nothing and accept no parameters in Objective-C and Swift?

。_饼干妹妹 提交于 2021-02-17 05:50:08
问题 I'm having trouble understanding closure syntax in Swift and Objective-C. Can someone tell me all possible ways in both languages to write a closure which accepts no arguments and returns nothing? 回答1: In Objective-C language void (^closureA)(void) = ^{ }; In Swift language let closureB: () -> () let closureC: () -> Void 回答2: Since you ask for all and since C is within Objective-C's reach and since you specify no parameters, this also gets the job done. void ( * f ) ( void ); // C function