Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.background modifier with contained shape causes view to expand to full vertical size of the screen #402

Closed
foscomputerservices opened this issue May 14, 2021 · 1 comment · Fixed by #429
Assignees
Labels
bug Something isn't working

Comments

@foscomputerservices
Copy link
Contributor

When a shape (say a RoundedRectangle()) is added to the background modifier, the view fills the entire vertical height of the screen.

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .background(RoundedRectangle(cornerRadius: 10.0).fill(Color.red))
    }
}

Expected behavior

On iOS the background modifier does not change the view size, so I expected the size to remain the size of Text.

Screenshots
Attached

Screen Shot 2021-05-14 at 6 06 19 PM

Workaround

In this particular example, the following code can be used to work around the issue:

        Text("Hello, world!")
            .background(Color.red)
            .cornerRadius(10)

NOTE: This issue was previously described along with many other issues in issue #336. I wanted to provide a contrite example.

@foscomputerservices foscomputerservices added the bug Something isn't working label May 14, 2021
@carson-katri
Copy link
Member

carson-katri commented Jul 16, 2021

Just dumping this here until I have time to open a PR, but this CSS should be able to fix both background and overlay layouts:

.zstack {
  display: inline-grid;
  grid-template-columns: auto auto;
}

.foreground {
  grid-area: a;
}

.background {
  /* The interesting part */
  width: 0;
  min-width: 100%;
  height: 0;
  min-height: 100%;
  
  grid-area: a;
  overflow: hidden;
  
  background-color: red;
}

It wouldn't actually apply to ZStack, just the background and overlay layouts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

3 participants