27 C
Canberra
Saturday, March 7, 2026

The way to change vertical alignment of particular person gadgets in SwiftUI HStack?


I’ve the next code.

struct ContentView: View {
    @State var textInput: String = "Testing"

    non-public let inputCornerRadius: CGFloat = 24

    var physique: some View {
        HStack(alignment: .backside, spacing: 8) {
            TextField("Chat", textual content: $textInput, axis: .vertical)
                .lineLimit(1...9)
                .textFieldStyle(.plain)

            Button(motion: ship) {
                Picture(systemName: "paperplane.fill")
                    .foregroundStyle(.background)
                    .imageScale(.medium)
                    .padding(10)
                    .background(
                        Circle()
                            .fill(Shade(.label))
                        )
            }
        }
        .padding(.vertical, 8)
        .padding(.main, 10)
        .padding(.trailing, 6)
        .background(
            RoundedRectangle(cornerRadius: inputCornerRadius, model: .steady)
                .fill(Shade(.secondarySystemBackground))
        )
        .clipShape(RoundedRectangle(cornerRadius: inputCornerRadius, model: .steady))
        .padding(.horizontal, 10)
    }

    non-public func ship() {}
}

The way to change vertical alignment of particular person gadgets in SwiftUI HStack?

The problem is that the textual content subject is aligned to the underside. I need it to be centered. Right here is the difficult half tho, I need the ship button to at all times be aligned to the underside (or heart if it is only a single line).

If I alter the HStack alignment to .heart, it really works precisely how I need for single line textual content, however as soon as it will get to be a number of strains, the ship button is centered as a substitute of aligned to the underside.

Screenshot 2

So listed below are my necessities:

  • For single lined textual content, each textual content enter and ship button are centered vertically.
  • For multi lined textual content, the ship button is aligned to the underside of the grey background (plus padding).

Issues I’ve tried:

  • Setting alignment to .heart (I defined why that would not work above)
  • Wrapping the TextField in one other HStack that’s aligned heart (the guardian continues to be aligned backside). This did not have any impact.
  • Added a VStack across the TextField, with Spacers on the prime and backside. This had the impact of constructing all the view top stretch to match the guardian views top (which I do not need).
  • Added .alignmentGuide(.backside) { d in d[.bottom] } to the Button, and set the alignment to .heart. Nonetheless had no impact, and the button remained centered.

Ideally I would wish to keep away from doing textual content top calculations and all of that. It seems like this must be a reasonably easy UI I am attempting to construct, so it does not appear lifelike to overcomplicate it.

How can I get the Button to at all times be aligned to the underside, whereas the TextField stays centered?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles