skip to Main Content

SwiftUI Divider behaves as if it has frame(maxWidth: .infinity). So it widens its parent view.

How can I prevent Divider from widening its parent view in the below code?

HStack {
    VStack {
        Text("AAA")
        Divider()
        Text("BBB")
    }
    .background(Color.orange)

    Text("BodyBodyBodyBodyBodyBodyBodyBodyBodyBody")
    .frame(maxWidth: .infinity)
    .background(Color.blue)
}

Expected

enter image description here

Actual

enter image description here

2

Answers


  1. Try this:

    HStack {
        VStack {
            Text("AAABBCCDD")
            Divider()
            Text("BBB")
        }
        .fixedSize() //<- Added here
        .background(Color.orange)
        
        Text("BodyBodyBodyBodyBodyBodyBodyBodyBodyBody")
        .frame(maxWidth: .infinity)
        .background(Color.blue)
    }
    

    Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search