SwiftUI列表:隐藏默认Disclosure Indicator并保持单元格颜色不变
要实现你的需求,只需对NavigationLink和列表行做两处关键修改:
1. 隐藏默认披露指示器
iOS 16+ 可直接使用.disclosureIndicator(.hidden)修饰符隐藏系统默认指示器;若需兼容更低版本,可将NavigationLink与单元格内容分离,用自定义手势触发跳转。
2. 移除单元格高亮并保留原有颜色
给NavigationLink添加.buttonStyle(PlainButtonStyle())取消默认点击高亮,同时通过listRowBackground维持单元格的透明或自定义底色。
修改后的完整代码
import SwiftUI struct CurrencyHistoryView: View { @State private var textHeight: Double = 20 let listRowPadding: Double = 5 let listRowMinHeight: Double = 65 var listRowHeight: Double { max(listRowMinHeight, textHeight + 2 * listRowPadding) } var dataArray: [CurrencyData] = [ CurrencyData(date: "Room Rent", currency: "10000"), CurrencyData(date: "School Fee", currency: "6000"), CurrencyData(date: "Transport", currency: "2200"), CurrencyData(date: "Food", currency: "4300"), CurrencyData(date: "Electric city bill", currency: "2700"), CurrencyData(date: "Fuel", currency: "2600"), CurrencyData(date: "Extra", currency: "1900") ] var body: some View { GeometryReader { _ in ZStack { Color.black VStack { List { Section { ForEach(dataArray) { array in NavigationLink(destination: Text("")) { VStack { HStack(alignment: .center) { SAGenericText(textString: array.date, textColor: Color.SAColor.colorBlack100White90, textFont: SAFontConstants.getFont(for: .regular, with: .size14)) Spacer() HStack(alignment: .bottom, spacing: 6) { SAGenericText(textString: "Rs", textColor: Color.SAColor.colorBlack55White50, textFont: SAFontConstants.getFont(for: .regular, with: .size14)) .padding(.bottom, 1) SAGenericText(textString: array.currency, textFont: SAFontConstants.interSemiBoldLargeText!) } Image(systemName: "chevron.right") .renderingMode(.original) } .padding(.bottom, 5) Rectangle().fill(Color.SAColor.colorBlack10White10).frame(height: 1) } } // 隐藏默认披露指示器 .disclosureIndicator(.hidden) // 取消点击高亮,保留单元格原有颜色 .buttonStyle(PlainButtonStyle()) .listRowBackground(Color.clear) .listRowSeparator(.hidden) } } } .frame(height: CGFloat(dataArray.count) * CGFloat(self.listRowHeight)) .listStyle(GroupedListStyle()) } .cornerRadius(7) } } } } // 补充代码中未定义的依赖结构 struct CurrencyData: Identifiable { let id = UUID() var date: String var currency: String } extension Color { enum SAColor { static let colorBlack100White90 = Color.white static let colorBlack55White50 = Color.gray static let colorBlack10White10 = Color.gray.opacity(0.1) } } struct SAFontConstants { static func getFont(for weight: Font.Weight, with size: CGFloat) -> Font { return Font.system(size: size, weight: weight) } static let interSemiBoldLargeText = Font.system(size: 16, weight: .semibold) } #Preview { NavigationStack { CurrencyHistoryView() } }
iOS 15及以下版本兼容方案
若需支持iOS 15,.disclosureIndicator(.hidden)不可用,可改用以下方式:将NavigationLink放在单元格背景中并透明化,用自定义点击区域控制跳转:
ForEach(dataArray) { array in VStack { // 单元格内容 HStack(alignment: .center) { SAGenericText(textString: array.date, textColor: Color.SAColor.colorBlack100White90, textFont: SAFontConstants.getFont(for: .regular, with: .size14)) Spacer() HStack(alignment: .bottom, spacing: 6) { SAGenericText(textString: "Rs", textColor: Color.SAColor.colorBlack55White50, textFont: SAFontConstants.getFont(for: .regular, with: .size14)) .padding(.bottom, 1) SAGenericText(textString: array.currency, textFont: SAFontConstants.interSemiBoldLargeText!) } Image(systemName: "chevron.right") .renderingMode(.original) } .padding(.bottom, 5) Rectangle().fill(Color.SAColor.colorBlack10White10).frame(height: 1) } .background( NavigationLink(destination: Text("")) { EmptyView() } .opacity(0) // 隐藏默认NavigationLink外观 ) .contentShape(Rectangle()) // 确保整个单元格可点击 .listRowBackground(Color.clear) .listRowSeparator(.hidden) }
内容的提问来源于stack exchange,提问作者Ios Developer
相关产品推荐
相关产品推荐

