Загрузка...

Creating Uneven Split Layouts in SwiftUI: A Guide to Using GeometryReader

Learn how to create unevenly split layouts in SwiftUI using the `GeometryReader`. Discover how to assign heights by percentage for more flexible designs.
---
This video is based on the question https://stackoverflow.com/q/66797796/ asked by the user 'Cranie' ( https://stackoverflow.com/u/3904988/ ) and on the answer https://stackoverflow.com/a/66798975/ provided by the user 'lorem ipsum' ( https://stackoverflow.com/u/12738750/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: SwiftUI change the layout to split the view into uneven sections

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Creating Uneven Split Layouts in SwiftUI: A Guide to Using GeometryReader

When developing applications with SwiftUI, you may sometimes need to create layouts that divide the screen space into uneven sections. Imagine you have two text elements and you want the first one to occupy one-third of the screen while the second one takes up two-thirds. Instead of default 50/50 splits, you can achieve this by using the powerful GeometryReader.

The Challenge

You may start with a basic layout like this:

[[See Video to Reveal this Text or Code Snippet]]

This code creates two Text views that split the screen evenly, with each element occupying 50% of the available space. However, in this instance, you want:

The first Text view to occupy 1/3 of the height.

The second Text view to occupy 2/3 of the height.

This is where GeometryReader comes in handy.

What is GeometryReader?

GeometryReader is a container view that dynamically reflects its size and coordinate space. This means you can write your layout code in a way that respects the dimensions of its parent view, which is perfect for percentage-based layouts.

Implementing the Layout

Here’s how you can utilize GeometryReader to create an uneven split layout:

[[See Video to Reveal this Text or Code Snippet]]

Breakdown of the Code

GeometryReader: It provides the dimensions of its parent view (geo.size.height).

VStack: This is a vertical stack that will hold both Text views and the Divider.

Frame Modifiers:

For the first Text view, we set the height to geo.size.height * (1/3), allowing it to occupy one-third of the screen height.

The second Text view similarly has its height set to geo.size.height * (2/3), allowing it to occupy two-thirds.

Additional Considerations

Customization: The GeometryReader offers flexibility. You can easily adjust the height ratios or add additional views as needed.

Layouts: Apply the same techniques to images, market data, or any other views in your SwiftUI application to create varied and responsive layouts based on available space.

Conclusion

Using GeometryReader for uneven splits in SwiftUI not only simplifies your layout logic but also enhances the adaptability of your views to different screen sizes. This approach allows you to create a more aesthetically pleasing and functional design tailored to your app's needs. So, the next time you find yourself needing an uneven layout, remember this powerful tool!

With this knowledge, get creative and customize your layouts to design stunning user interfaces in your SwiftUI applications.

Видео Creating Uneven Split Layouts in SwiftUI: A Guide to Using GeometryReader канала vlogize
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки