Conditionally use VStack or HStack in SwiftUI

Conditionally use VStack or HStack in SwiftUI

By Atikur Rehman, Senior iOS Developer at Evangelist Apps

In your SwiftUI app, there might be cases where you might want to stack some views horizontally or vertically based on certain conditions or even while rotating the device. While we can use a condition to wrap the views in either VStack or HStack.

No alt text provided for this image

The above approach is not ideal and leads to code duplication (as you notice above).

A better solution would be to create an AdaptiveStack view, which accepts a boolean parameter to decide whether to stack the child views horizontally or vertically.

No alt text provided for this image

Then we could we that AdaptiveStack view as below -

No alt text provided for this image

 Please share your thoughts and suggestions on this article.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了