Skip to content

Instantly share code, notes, and snippets.

@kkukelka
Last active April 6, 2023 06:27
Show Gist options
  • Save kkukelka/003d2e36a9363aa843450a3c429d1d56 to your computer and use it in GitHub Desktop.
Save kkukelka/003d2e36a9363aa843450a3c429d1d56 to your computer and use it in GitHub Desktop.
Swift UI Tidbits

ZStack rendering

Views within ZStack are rendered back-to-front, with the first view being placed at the back

fixedSize()

use fixedSize() to neglect the view size proposed to a view by its parent

HStack {
    Button(action: {}) { Text("Cancel").fixedSize() }
        .padding().frame(maxWidth: .infinity)
    Divider()
    Button(action: {}) {  Text("Delete").fixedSize() }
        .padding().frame(maxWidth: .infinity)

}.fixedSize(horizontal: false, vertical: true)

Custom Colors via Color Set

Navigate to Assets.xcassets in your project folder Right click into the panel to the left and select New Color Set https://betterprogramming.pub/custom-colors-and-modifiers-in-swiftui-a093c243c126

lazy variables

  • lazy var (cannot be let)
  • enables just-in-time calculation when variable is actually needed
struct Person {
    lazy var fibonacciOfAge: Int = { // need to declare data type upfront (since value is created by evaluation)
        fibonacci(of: self.age) // use self inside function block
    }() // call function via ()

    func fibonacci(of num: Int) -> Int {...}
}

https://www.hackingwithswift.com/example-code/language/what-are-lazy-variables

empty loadView in NSViewController

 on macOS we need to override loadView to prevent error:
 "-[NSNib _initWithNibNamed:bundle:options:] could not load the nibName"
 this is due to the default implementation of NSViewController's loadView()
 ref: https://sarunw.com/posts/how-to-initialize-nsviewcontroller-programmatically-without-nib/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment