3 ms·
How's this? It's not quite "screen width" if you make the screen wider than it is tall, but I could make it stick to that if you really want I guess. struct
by saagarjha 4y ago
How's this? It's not quite "screen width" if you make the screen wider than it is tall, but I could make it stick to that if you really want I guess.
struct ContentView: View {
var body: some View {
ZStack {
Rectangle()
.fill(Color.red)
Rectangle()
.fill(Color.blue)
.frame(width: 100, height: 100)
HStack {
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 100, height: 100)
Spacer()
}
Spacer()
}
.padding(10)
}
.aspectRatio(1, contentMode: .fit)
.padding(10)
}
}
- alexashka 4y agoYour answer is wrong. You've failed the fizzbuzz :)
- saagarjha 4y agoI generally expect interviewers to tell me what the problem is ;)
- alexashka 4y agoMy apologies, you've successfully passed FizzBuzz, in a rather unwieldy way by relying on nesting vstacks, hstacks and adding spacers. The larger point being - placing squares on a page went from drag and dropping and setting a few constraints, to now doing zstack/hstack/vstack/spacer wizardry.
- myko 4y ago> drag and dropping and setting a few constraints, to now doing zstack/hstack/vstack/spacer wizardry. Very few iOS developers I know did "drag and drop"/IB development, layouts are typically done in code even in UIKit. The zstack/hstack/vstack bits don't seem wizardy at all if you're used to looking at layouts in code.
- saagarjha 4y agoI think I'd ideally want to do this with some alignment constraints but I don't see this as being far more difficult than setting constraints manually. Doing this correctly is actually somewhat non-trivial because you'll want the square to respond to rotation appropriately, etc. which will require less-than-or-equal constraints.
- andrekandre 4y agoactually thats a good point i wonder how one could go about implementing custom layout in swiftui, something like `layoutSubViews` so we could implement something simplified like: View(layout: .grid) { ... } or View(layout: .stack(.vertical)) { ... }
- emehex 4y agoPerhaps you can post a photo of what you're looking for? Because your instructions aren't super clear :)