4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】HStackを下寄せにした時の要素の下辺を揃える

Last updated at Posted at 2023-04-28

はじめに

タイトルでは意味がわからないと思うので説明します。

今回何をしたいのか

要素を横並びにしてalignment.bottomを指定しています

import SwiftUI

struct ContentView: View {
    var body: some View {
        HStack(alignment: .bottom, spacing: 10) {
            Text("タイトル")
                .font(.system(size: 50, weight: .bold))
                .foregroundColor(.primary)
            Text("サブタイトル")
                .font(.system(size: 15, weight: .regular))
                .foregroundColor(.secondary)
        }
        .background(Color.cyan)
    }
}

上記のコードだとこのようになります。
スクリーンショット 2023-04-28 22.19.27.png

下を揃えたいのに揃っていません。
スクリーンショット 2023-04-28 22.20.48.png

これを揃えていきたいと思います。

実装

import SwiftUI

struct ContentView: View {
    var body: some View {
        HStack(alignment: .bottom, spacing: 10) {
            Text("タイトル")
                .font(.system(size: 50, weight: .bold))
                .foregroundColor(.primary)
            Text("サブタイトル")
                .font(.system(size: 15, weight: .regular))
                .foregroundColor(.secondary)
+               .baselineOffset(7)
        }
        .background(Color.cyan)
    }
}

ちゃんと揃いました。
スクリーンショット 2023-04-28 22.25.15.png

ドキュメント

おわり

たまたま見つけましたー

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?