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?

普通にListにHStackを入れていくとセパレータが画像の端まで伸びない問題の回避方法

0
Posted at

RowはただのHStackです。

//
//  ListRowView.swift
//  SwiftUIList
//
//  Created by tsu-na-gu on 2026/04/12.
//

import SwiftUI

struct BasicImageRow: View {
    let restraunt: Restaurant
  
    
    var body: some View {
            HStack {
                Image(restraunt.image)
                    .resizable()
                    .frame(width: 40, height: 40)
                    .cornerRadius(6)
                Text(restraunt.name)
            }

    }
}

#Preview {
    let restraunt = Restaurant(name: "Cafe Deadend", image: "cafedeadend")

    BasicImageRow(restraunt: restraunt)
}

これをそのままListに入れると画像の端までセパレーターが伸びない
スクリーンショット 2026-04-12 1.55.52.png

その回避方法は行毎に

swift.
alignmentGuide(.listRowSeparatorLeading) { _ in 0 }

をいれる事。

ContentView全体は

//
//  ContentView.swift
//  SwiftUIList
//
//  Created by tsu-na-gu on 2026/04/12.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            ForEach(restaurants.indices, id: \.self) { index in
                    if (0...1).contains(index) {
                        FullImageRow(restraunt: restaurants[index])
                    } else {
                        BasicImageRow(restraunt: restaurants[index])
                    }
                }
                .alignmentGuide(.listRowSeparatorLeading) { _ in
                    0
                }
            }
            .listStyle(.plain)
    }
}

#Preview {
    ContentView()
}

これで端まで引かれます。

スクリーンショット 2026-04-12 2.23.23.png

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?