tng
tng

Reputation: 4346

Aligning the height of views in SwiftUI

I have a SwiftUI view which is an HStack of two views: 1. A simple Text view, which acts as sort of a label, and is always a single line 2. A custom UIKit view which is a UITextView with lots of custom formatting

My desire is to restrict the height of the UITextView to match the height of the Text view (single line, height dependent on system fonts, etc). I've tried many ways to go about this, but I can't seem to find something that works. GeometryReader seems to only pass parent attributes to child, but doesn't solve the "sibling" issue.

Any ideas or insight?

Upvotes: 1

Views: 266

Answers (1)

Asperi
Asperi

Reputation: 258441

Here is a demo of possible layout. Tested with Xcode 11.4 / iSO 13.4

demo

Note: .border and .padding are added just for demo & better visibility. Important places are marked in comment. MultilineTextView is a simple representable of UITextView

struct DemoFixedToLabel: View {
    var body: some View {
        HStack {
            Text("Some Text").font(Font.system(.title))
                .padding()
                .border(Color.blue)
                .fixedSize()             // << here !!
            MultilineTextView(text: .constant("My desire is to restrict the height of the UITextView to match the height of the Text"))
                .border(Color.green)
        }
        .padding()
        .border(Color.red)
        .fixedSize(horizontal: false, vertical: true)    // << here !!
    }
}

Upvotes: 1

Related Questions