xcode iOS 15中的SwiftUI垂直列表节头填充

envsm3lx  于 2023-08-07  发布在  iOS
关注(0)|答案(3)|浏览(102)

我有一个ListSections

List {
        ForEach((1...3), id: \.self) { _ in
            Section(
                header: Text("My Section")
                            .font(.system(.title3))
                            .fontWeight(.bold)
                            .foregroundColor(.primary),
                content: {
                    ForEach((1...5), id: \.self) { _ in
                        Text("My Row")
                    }
                }
            )
        }
    }

字符串
以下是在iOS 14.5的iPhone 8模拟器上的结果:


的数据
下面是iOS 15的iPhone 8模拟器的结果:



我希望iOS15列表等于iOS14.5一个。我可以通过将.listStyle(PlainListStyle())添加到List来删除水平填充,但该部分的头部仍然有不同的垂直填充。


是否有一种方法可以像iOS14.5一样具有相同的垂直标题填充?

工作环境:

  • iOS 15 RC
  • XCode 13 RC
brqmpdu1

brqmpdu11#

最后,最适合我的是加上

if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = 0
}

字符串
在AppDelegate didFinishLaunchingWithOptions函数中

w1e3prcc

w1e3prcc2#

在iOS 15和16上使用SwiftUIIntrospect 0.10.0测试

支持SwiftUI List
此修饰符将删除iOS 15上UITableView的标头的填充

.introspect(.list(style: .plain), on: .iOS(.v15)) { tableView in
    tableView.sectionHeaderTopPadding = 0
}

字符串
此修饰符将删除iOS 16上UICollectionView头的填充

.introspect(.list(style: .plain), on: .iOS(.v16)) { collectionView in
    let identifier = "ToolbarConfigured"
    guard collectionView.focusGroupIdentifier != identifier else { return }
    collectionView.focusGroupIdentifier = identifier
    
    var configuration = UICollectionLayoutListConfiguration(appearance: .plain)
    configuration.headerMode = .supplementary
    configuration.headerTopPadding = .zero
    configuration.showsSeparators = false
    configuration.backgroundColor = .clear
    let layout = UICollectionViewCompositionalLayout.list(using: configuration)
    collectionView.setCollectionViewLayout(layout, animated: false)
}


此代码将删除iOS 15iOS 16标题下出现的灰色背景。它需要UIScrollView,UICollectionView和UITableView都符合UIScrollView。工程与单一的标题,始终提出。

.introspect(.scrollView, on: .iOS(.v15, .v16)){ scrollView in
    guard let headerBG = scrollView.subviews
        .first(where: { $0.frame.height == 220 })?
        .subviews
        .first(where: { String(describing: type(of: $0)) == "_UISystemBackgroundView" }) else { return }
    guard headerBG.alpha != 0 else { return }
    headerBG.alpha = 0
}

gg0vcinb

gg0vcinb3#

使用.listStyle()修饰符将列表的样式更改为GroupedListStyle

.listStyle(GroupedListStyle())

字符串
标题无灰色背景

.listStyle(PlainListStyle())

更多信息

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

相关问题