阅读 161

SwiftUI -- 取消List的分割线

iOS 13 List 的底层是 UITableView,可以直接设置 UITableView 的 separatorStyle 为 none 来去除分割线
在 iOS 14中 List 的实现不在是 UITableView 了,可以设置 .listStyle(SidebarListStyle()),但会有很大的边距。也可以用 Scrollview + LazyVStack 替代 List,

The stack is "lazy," in that the stack view doesn't create items until it needs to render them onscreen.

类似的还有 LazyHStack、LazyVGrid、LazyHGrid

struct PokemonList: View {
    
    var body: some View {
        if #available(iOS 14.0, *) {
            ScrollView {
                LazyVStack(content: {
                    ForEach(PokemonViewModel.all) { pokemon in
                        PokemonInfoRow(model: pokemon, expanded: true)
                    }
                })
            }
//            List(PokemonViewModel.all){ pokemon in
//                PokemonInfoRow(model: pokemon, expanded: true)
//            }.listStyle(SidebarListStyle())
        } else {
            List(PokemonViewModel.all){ pokemon in
                PokemonInfoRow(model: pokemon, expanded: true)
            }.modifier(ListRemoveSeparator())
        }
    }
}

struct ListRemoveSeparator: ViewModifier {
    func body(content: Content) -> some View {
        content
            .onAppear(perform: {
                UITableView.appearance().tableFooterView = UIView()
                UITableView.appearance().separatorStyle = .none
            })
            .onDisappear(perform: {
                UITableView.appearance().tableFooterView = nil
                UITableView.appearance().separatorStyle = .singleLine
            })
    }
}

参照: stackoverflow

作者:jancywen

原文链接:https://www.jianshu.com/p/da8c418b1157

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐