4 ms·
If you like SwiftUI, I challenge you to a SwiftUI fizzbuzz: a square, screen width, 10x padding, red background. inside it, a little square, centered and a li
by alexashka 4y ago
If you like SwiftUI, I challenge you to a SwiftUI fizzbuzz:
a square, screen width, 10x padding, red background.
inside it, a little square, centered and a little square, top left corner, 10px padding, blue background for both.
Tell me that was easy, so that I know you're lying :)
- BakaRakuda 4y agoI'm late to the party, but how about this: struct ContentView: View { var body: some View { ZStack { Color.red Color.blue .frame(width: 50, height: 50) .padding(10) Color.blue .frame(width: 50, height: 50) .position(x: 50/2, y: 50/2) .padding(10) } .aspectRatio(1, contentMode: .fit) .padding(10) } }
- emehex 4y agoTook me 5 minutes. Copy and paste this into a playground: https://gist.github.com/maxhumber/1dbdf0d9c4539a8e145b3c630c6da0b3 https://gist.github.com/maxhumber/1dbdf0d9c4539a8e145b3c630c...
- deleted 4y ago[deleted]
- alexashka 4y agoYour answer is wrong. You've failed the fizzbuzz :) Change frame (screen width) to 300, 500 to see why.
- emehex 4y agoGood catch. Fixed with an extra ZStack.
- alexashka 4y agoGeometryReader was not part of fizzbuzz. Replace geo.size.width * 0.1 with 10 to see your code break. Now tell me, why did it break? Do you even know? Because I don't. I don't know why half the shit works as it does, and I've written non-trivial SwiftUI code for 6 months. I did not have this problem with UIKit and interface builder.
- emehex 4y agoGeometryReader is a pretty critical part of the SwiftUI framework. I'd recommend getting familiar with it! Saying "you can't use GeometryReader" would be like saying you can't use UIKit's Auto Layout...
- deleted 4y ago[deleted]
- b3morales 4y agoNow please see how it looks on a device: https://postimg.cc/crtKHSTG https://postimg.cc/crtKHSTG
- saagarjha 4y agoHow'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.