News for hackers. Stuff that matters.
Re:SwiftUI in 2022 by alexashka on Wednesday May 25, 2022 @11:28AM (#31505950)
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 :)

Re:SwiftUI in 2022 by saagarjha on Wednesday May 25, 2022 @01:04PM (#31507294)
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)
      }
  }
Re:SwiftUI in 2022 by alexashka on Wednesday May 25, 2022 @04:15PM (#31509922)
Your answer is wrong. You've failed the fizzbuzz :)
Re:SwiftUI in 2022 by emehex on Wednesday May 25, 2022 @05:07PM (#31510562)
Perhaps you can post a photo of what you're looking for? Because your instructions aren't super clear :)
Re:SwiftUI in 2022 by saagarjha on Wednesday May 25, 2022 @04:36PM (#31510195)
I generally expect interviewers to tell me what the problem is ;)
Re:SwiftUI in 2022 by alexashka on Wednesday May 25, 2022 @05:08PM (#31510571)
My 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.

Re:SwiftUI in 2022 by myko on Saturday May 28, 2022 @08:56PM (#31545211)
> 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.

Re:SwiftUI in 2022 by saagarjha on Thursday May 26, 2022 @06:49AM (#31516835)
I 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.
Re:SwiftUI in 2022 by andrekandre on Thursday May 26, 2022 @09:58AM (#31518404)
actually 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)) { ... }
Re:SwiftUI in 2022 by BakaRakuda on Saturday May 28, 2022 @01:35PM (#31542156)
I'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)
    }
  }
Re:SwiftUI in 2022 by emehex on Wednesday May 25, 2022 @11:41AM (#31506119)
Took me 5 minutes. Copy and paste this into a playground: https://gist.github.com/maxhumber/1dbdf0d9c4539a8e145b3c630c...
Re:SwiftUI in 2022 by alexashka on Wednesday May 25, 2022 @11:53AM (#31506307)
Your answer is wrong. You've failed the fizzbuzz :)

Change frame (screen width) to 300, 500 to see why.

Re:SwiftUI in 2022 by emehex on Wednesday May 25, 2022 @01:12PM (#31507421)
Good catch. Fixed with an extra ZStack.
Re:SwiftUI in 2022 by alexashka on Wednesday May 25, 2022 @04:48PM (#31510336)
GeometryReader 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.

Re:SwiftUI in 2022 by emehex on Wednesday May 25, 2022 @05:03PM (#31510524)
GeometryReader 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...

Re:SwiftUI in 2022 by b3morales on Wednesday May 25, 2022 @12:05PM (#31506486)
Now please see how it looks on a device: https://postimg.cc/crtKHSTG