This app use the math_operation library.
Apply AutoLayout via Geometry Reader
- SwiftUI
- Text
- Button
- Render(diff)
- State / Binding
- Expression Library
- SwiftUI AutoLayout
- Peer Soft Skill
var buttonsView: some View {
let buttons: [[(String, () -> Void)]] = [
[("7", { self.buttonClickEvent("7") }), ("8", { self.buttonClickEvent("8") }), ("9", { self.buttonClickEvent("9") }), ("C", { self.buttonClickEvent("C") }), ("AC", { self.buttonClickEvent("AC") })],
[("4", { self.buttonClickEvent("4") }), ("5", { self.buttonClickEvent("5") }), ("6", { self.buttonClickEvent("6") }), ("+", { self.buttonClickEvent("+") }), ("-", { self.buttonClickEvent("-") })],
[("1", { self.buttonClickEvent("1") }), ("2", { self.buttonClickEvent("2") }), ("3", { self.buttonClickEvent("3") }), ("*", { self.buttonClickEvent("*") }), ("/", { self.buttonClickEvent("/") })],
[("0", { self.buttonClickEvent("0") }), (".", { self.buttonClickEvent(".") }), ("00", { self.buttonClickEvent("00") }), ("=", { self.buttonCalculate() }), ("",{self.buttonCalculate()})]
]
return GeometryReader { geometry in
VStack(spacing: 0) {
ForEach(0..<buttons.count, id: \.self) { rowIndex in
HStack(spacing: 0) {
ForEach(0..<buttons[rowIndex].count, id: \.self) { columnIndex in
let button = buttons[rowIndex][columnIndex]
Button(action: button.1) {
Text(button.0)
.font(.largeTitle)
.foregroundColor(.white)
.frame(width: self.calculateButtonWidth(containerWidth: geometry.size.width, buttonCount: buttons[rowIndex].count), height: calculateButtonHeight(containerHeight: geometry.size.height, buttonCount: buttons.count))
.background(self.buttonColor(button.0))
}
}
}
}
}
.frame(width: geometry.size.width)
}
}