Skip to main content

How to make two views the same width or height

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to make two views the same width or height 관련

SwiftUI by Example

Back to Home

How to make two views the same width or height | SwiftUI by Example

How to make two views the same width or height

Updated for Xcode 15

SwiftUI makes it easy to create two views that are the same size, regardless of whether you want the same height or the same width, by combining a frame() modifier with fixedSize() – there's no need for a GeometryReader or similar.

On iOS, the key is to give each view you want to size an infinite maximum height or width, which will automatically make it stretch to fill all the available space. You then apply fixedSize() to the container they are in, which tells SwiftUI those views should only take up the space they need. The result is that SwiftUI figures out the least amount of space the views need, then allows them to take up that full amount – the two views will always match their sizes no matter what they contain.

Here's an example showing how to make two text views have the same height even though they have very different text lengths. Thanks to the frame() and fixedSize() combination both text views are laid out at the same size:

HStack {
    Text("This is a short string.")
        .padding()
        .frame(maxHeight: .infinity)
        .background(.red)

    Text("This is a very long string with lots and lots of text that will definitely run across multiple lines because it's just so long.")
        .padding()
        .frame(maxHeight: .infinity)
        .background(.green)
}
.fixedSize(horizontal: false, vertical: true)
.frame(maxHeight: 200)

Download this as an Xcode projectopen in new window

A red rectangle containing a one line sentence beside a green rectangle containing a two line sentence. Both rectangles are the same height.
A red rectangle containing a one line sentence beside a green rectangle containing a two line sentence. Both rectangles are the same height.

This approach works just as well when you want to make two views have the same width:

VStack {
    Button("Log in") { }
        .foregroundStyle(.white)
        .padding()
        .frame(maxWidth: .infinity)
        .background(.red)
        .clipShape(Capsule())

    Button("Reset Password") { }
        .foregroundStyle(.white)
        .padding()
        .frame(maxWidth: .infinity)
        .background(.red)
        .clipShape(Capsule())
}
.fixedSize(horizontal: true, vertical: false)
A red capsule containing the white text “Log In” above a similar capsule containing “Reset Password”. Both capsules are the same width.
A red capsule containing the white text “Log In” above a similar capsule containing “Reset Password”. Both capsules are the same width.

There are many other significantly more complex solutions to this same problem, which is quite strange given how well the simple solution works for most people. I first learned this solution from Becky Hansmeyeropen in new window and now I use nothing else!

Similar solutions…
How to make two gestures recognize at the same time using simultaneousGesture() | SwiftUI by Example

How to make two gestures recognize at the same time using simultaneousGesture()
How to customize a view's width in NavigationSplitView | SwiftUI by Example

How to customize a view's width in NavigationSplitView
How to fill and stroke shapes at the same time | SwiftUI by Example

How to fill and stroke shapes at the same time
Two-way bindings in SwiftUI | SwiftUI by Example

Two-way bindings in SwiftUI
How to create a two-column or three-column layout with NavigationSplitView | SwiftUI by Example

How to create a two-column or three-column layout with NavigationSplitView

이찬희 (MarkiiimarK)
Never Stop Learning.