0xLet / SwiftUIKit

📱 UIKit code that is fun to write
https://0xlet.github.io/SwiftUIKit/
MIT License
74 stars 6 forks source link

Improve ZStack usage #113

Closed 0xLeif closed 4 years ago

0xLeif commented 4 years ago

Correct ZStack usage in SwiftUIKit ATM

let innerView = View(backgroundColor: .blue).frame(height: 100, width: 100)
let stack = ZStack {
    [
        innerView
    ]
}

view.embed { 
    stack
}

NSLayoutConstraint.activate([
    innerView.centerXAnchor.constraint(equalTo: stack.centerXAnchor),
    innerView.centerYAnchor.constraint(equalTo: stack.centerYAnchor)
])

Expected:

let innerView = View(backgroundColor: .blue).frame(height: 100, width: 100)
let stack = ZStack {
    [
        innerView
    ]
}
.activateLayoutConstraints {
    [
        innerView.centerXAnchor.constraint(equalTo: stack.centerXAnchor),
        innerView.centerYAnchor.constraint(equalTo: stack.centerYAnchor)
    ]
}

view.embed { 
    stack
}
0xLeif commented 4 years ago

Expected:

let innerView = View(backgroundColor: .blue).frame(height: 100, width: 100)
let stack = ZStack {
    [
        innerView
    ]
}

view.embed {
    stack
        .activateLayoutConstraints {
        [
            innerView.centerXAnchor.constraint(equalTo: stack.centerXAnchor),
            innerView.centerYAnchor.constraint(equalTo: stack.centerYAnchor)
        ]
    }
}

Since Variable used within its own initial value doesn't allow stack to be referenced:

let stack = ZStack {
    [
        innerView
    ]
}
.activateLayoutConstraints {
        [
            innerView.centerXAnchor.constraint(equalTo: stack.centerXAnchor),
            innerView.centerYAnchor.constraint(equalTo: stack.centerYAnchor)
        ]
}