【问题标题】:How to change ListStyle in List如何更改 List 中的 ListStyle
【发布时间】:2019-10-21 18:18:25
【问题描述】:

在 SwiftUI 中 List 似乎有一个名为 ListStyle 的属性。

如何更改列表的样式

struct ListView : View {
var body: some View {
    NavigationView {
    List(Item.create().identified(by: \.id)){ row in
        NavigationButton(destination: DetailsView(item: row)) {
            RowView(item: row)
        }
    }
    .listStyle(StaticMember<PlainListStyle.Member>.self) // error here
    .foregroundColor(.red)
    .navigationBarTitle(Text("List View"))
    .statusBar(hidden: false)
    }
  }
}

ListStyle协议的符合方是

  1. 轮播列表样式
  2. 默认列表样式
  3. GroupedListStyle
  4. PlainListStyle
  5. 边栏列表样式

但是我正在努力尝试为列表设置新样式 像这样使用它

.listStyle(StaticMember<PlainListStyle.Member>.self)

我尝试了很多方法,但是每个确认ListStyle 的样式都是结构,就像它们不是枚举值一样

有人知道如何更改List 的样式吗?

Xcode 中的错误

无法将“StaticMember.Type”(又名“StaticMember>.Type”)类型的值转换为预期的参数类型“StaticMember<_>”

使用:.listStyle(StaticMember&lt;PlainListStyle.Member&gt;)

Xcode 中的错误

无法将类型“(StaticMember).Type”(又名“StaticMember>.Type”)的值转换为预期的参数类型“StaticMember<_>”

使用:.listStyle(StaticMember&lt;PlainListStyle()&gt;).listStyle(StaticMember&lt;PlainListStyle.self&gt;)

Xcode 中的错误

'>' 不是后缀一元运算符

【问题讨论】:

  • 我试过还是同样的问题
  • @user28434 检查我用过的更新

标签: ios swift swiftui


【解决方案1】:

从 Xcode 11 beta 5 开始,Apple 需要以下内容,简要概述here

.listStyle(GroupedListStyle())

以下是对各种样式的细​​分,以及它们可以在 iOSwatchOS 之间使用的位置,以及它们的推出时间。

iOS 和 watchOS

在 iOS 13 和 watchOS 6 中引入:

  • PlainListStyle

  • ListStyle

  • DefaultListStyle

仅限 iOS

在 iOS 13 中引入:

  • GroupedListStyle

在 iOS 14 中引入:

  • InsetGroupedListStyle
  • InsetListStyle
  • SidebarListStyle

此问题的一些答案还包括特定于 watchOS 的样式,但未明确标记为此类,尽管该问题已标记为 iOS。为了完整性...

仅限 watchOS

在 watchOS 6 中引入:

  • CarouselListStyle

在 watchOS 7 中引入:

  • EllipticalListStyle

【讨论】:

    【解决方案2】:

    Xcode 13 更新

    因此,Apple 添加了一个扩展,使其语法类似于 Xcode 11 Beta 5 及更低版本:

    extension ListStyle where Self == GroupedListStyle {
    
        /// The list style that describes the behavior and appearance of a grouped
        /// list.
        ///
        /// On iOS, the grouped list style displays a larger header and footer than
        /// the ``ListStyle/plain`` style, which visually distances the members of
        /// different sections.
        public static var grouped: GroupedListStyle { get }
    }
    

    所以我们现在可以再次使用它:

    .listStyle(.grouped)
    

    Xcode 11 Beta 5 更新至 Xcode 12

    在 Xcode Beta 5 之后,listStyle(.grouped) 方法被弃用(直到 Xcode 12);现在 Apple 为每种样式创建了一个结构实现。所以你应该这样做: .listStyle(GroupedListStyle())。同样的方法也适用于其他可用的样式。

    pre beta 5 的旧实现

    只需.listStyle(.grouped)。对于其他列表样式使用

    • .carousel
    • .default
    • .plain
    • .sidebar

    基本上,您只是将ListStyle.grouped 传递给该方法,但是由于快速类型推断,您不需要指定结构。 每个静态成员都以这种方式工作。

    StaticMember 表示ListStyle 协议中有一个静态成员。声明是这样的。

    extension StaticMember where Base : ListStyle {
    
        /// A `ListStyle` that implements the system default grouped `List`
        /// interaction and appearance.
        public static var grouped: GroupedListStyle.Member { get }
    }
    

    【讨论】:

    • .plain for PlainListStyle
    【解决方案3】:

    在 Xcode 11.2.1 中,正确答案在下面。

    .listStyle(GroupedListStyle())
    
    Conforming Types  ->
    CarouselListStyle
    DefaultListStyle
    GroupedListStyle
    PlainListStyle
    SidebarListStyle
    

    参考:https://developer.apple.com/documentation/swiftui/liststyle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 2019-11-23
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多