TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Filling screen width with a button produces "warning run: Invalid frame dimension (negative or non-finite)."

Forums > SwiftUI

I believe I learned in a tutorial here that the way to make a button fill the available space was to do this:

Button(action: {
  //
}) {
  Label("Button Example")
    .frame(maxWidth: .infinity)
}

If I'm not mistaken this did not cause errors at the time I learned it. But recently I am getting this error:

warning run: Invalid frame dimension (negative or non-finite).

Now I'm reading elsewhere that the solution is to replace .infinity with nil, however doing this produces completely different and unwanted behavior. While .inifinity produces the desired result of filling the available space, nil produces the same result as .none, which is also identical to not applying the .frame modifier with a maxWidth at all.

Does anyone know what the correct way to do this is now? I'm assuming that this has changed in the latest updates, and that the suggestion of using nil is just outdated information. I'm testing on iOS 16 with Xcode 14.0 beta 3.

1      

Ok, I seem to have gotten the desired behavior without this error by using GeometryReader:

GeometryReader { geo in
  Button(action: {
    //
  }) {
    Label("Button Example")
      .frame(maxWidth: geo.size.width)
  }
}

While this produces the result I need, the code seems less than ideal compared to what was working before it started throwing this warning. If anyone knows a better way, I'd love to hear about it—thanks!

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.