0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Android】【iOS】左右対称なレイアウト

0
Posted at

はじめに

「画面の中央に固定のスペースを置いて、その左右にコンテンツを配置するというレイアウトを作ることになった。

宣言的UI(SwiftUIやJetpack Compose)でこれを単純に横並び(HStack や Row)で組んでしまうと、左右の文字数の違いによって中央の要素が押し出され、画面のど真ん中からズレてしまうというレイアウト崩れが起きてしまう。

どんなデータが来ても「左右均等(シンメトリー)」を維持するレイアウトを組むならどうなるか、SwiftUIとComposeそれぞれで試してみた。

SwiftUi

  • .frame(maxWidth: .infinity) と Spacer(minLength: 0) を組み合わせて左右を均等にする
struct SymmetricView: View {
    var body: some View {
        // 中央の隙間は固定Spacerで作るため spacing: 0 にする
        HStack(spacing: 0) { 
            // 左側ブロック(左寄せ)
            HStack(spacing: 5) {
                Text("あいうえお")
                    .lineLimit(1)
                
                // 左寄せを維持するために空間を埋める
                Spacer(minLength: 0) 
            }
            .frame(maxWidth: .infinity, alignment: .leading) 

            Spacer()
                .frame(width: UIScreen.main.bounds.width * 0.35)

            // 右側ブロック(右寄せ)
            HStack(spacing: 5) {
                // 右寄せを維持するために空間を埋める
                Spacer(minLength: 0) 
                
                Text("かきくけこさしすせそ")
                    .lineLimit(1)
            }
            .frame(maxWidth: .infinity, alignment: .trailing)
        }
    }
}
  • 左右の HStack に .frame(maxWidth: .infinity) を指定することで、真ん中の固定Spacerの幅を確保した後、残りの空間を左右のビューが1:1 になる
  • HStackの内側に Spacer(minLength: 0) を入れることで、テキストが短くてもそれぞれ画面の左端や右端に寄せられる

JetpackCompose

  • Modifier.weight() でレイアウトの重みを指定する
@Composable
fun SymmetricView() {
    val configuration = LocalConfiguration.current
    val centerSpaceWidth = (configuration.screenWidthDp * 0.35f).dp

    Row(modifier = Modifier.fillMaxWidth()) {
        
        // 左側ブロック
        Row(
            modifier = Modifier.weight(1f),
            // 左寄せ(※Startはデフォルトなので省略してもOK)
            horizontalArrangement = Arrangement.Start 
        ) {
            Text(
                text = "あいうえお",
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }

        // 中央の35%のSpacer
        Spacer(modifier = Modifier.width(centerSpaceWidth))

        // 右側ブロック
        Row(
            modifier = Modifier.weight(1f),
            // ★ 右寄せ
            horizontalArrangement = Arrangement.End 
        ) {
            Text(
                text = "かきくけこさしすせそ",
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }
    }
}
  • Composeでは親の Row の中で Modifier.weight(1f) を使って配分。固定幅(中央のSpacer)を引いた残りのスペースを指定された比率(今回は左 1f:右 1f)で均等に分割
  • SwiftUIの .lineLimit(1) は文字が溢れた時に自動で「...」と省略するが、Composeで省略表示をしたい場合は maxLines = 1 に加えて、overflow = TextOverflow.Ellipsis を明示的に指定する必要がる

まとめ

  • SwiftUi: HStack には中身を寄せる機能が乏しいので、内側にダミーの Spacer(minLength: 0) を仕込み、外側へ押しやる必要がある
  • Compose: 親である Row 自身が整列のためのプロパティを持っているので、わざわざSpacerで押しやらなくても、horizontalArrangement で指定するば整列できる
  • 個人的にはcomposeの方が無駄なSpacer置かない分スッキリして良いなぁという気持ち
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?