【问题标题】:Compatibility between SwiftUI's large title navigation bar and UIViewControllerRepresentable UITableViewControllerSwiftUI 大标题导航栏与 UIViewControllerRepresentable UITableViewController 的兼容性
【发布时间】:2020-11-01 06:21:44
【问题描述】:

为了在 SwiftUI 列表中实现向右滑动,我正在尝试使 UIViewControllerRepresentable UITableViewController。
但是 displayMode 为 .large 时与导航栏不兼容。

我猜,当卷轴移动时,它应该

  1. 调整导航栏的大小。
  2. 调整Scrollview或List的边框。

#2 不适用于我的表格视图,因此需要做一些事情。 我需要你的帮助。

[图片]

  1. 第一次渲染效果很好。
  2. 滚动后,表格视图上方有多余的空间
  3. 点击显示屏顶部滚动到顶部后,位置被破坏。
  4. 滚动后视图层次结构(第一次渲染后修复)

[代码]重现(从xcode创建swiftui项目后,替换内容 ContentView.swift 和下面的代码。)

import SwiftUI

final class TableViewWrapper: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UITableViewController {
        let viewController = UITableViewController()
        viewController.tableView.delegate = context.coordinator
        viewController.tableView.dataSource = context.coordinator

        return viewController
    }

    func updateUIViewController(_ uiViewController: UITableViewController, context: Context) {
        uiViewController.tableView.reloadData()
    }

    func makeCoordinator() -> Coordinator {
        Coordinator()
    }

    class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 100
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
            cell.backgroundColor = .black
            cell.textLabel!.textColor = .white
            cell.textLabel!.text = "Test"

            return cell
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            TableViewWrapper()
                .navigationBarTitle("Nav title", displayMode: .large)
        }
    }
}

【问题讨论】:

    标签: ios uitableview swiftui navigationbar uiviewcontrollerrepresentable


    【解决方案1】:

    我有一个解决办法。

        TableViewWrapper()
            .edgesIgnoringSafeArea(.top)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2021-05-14
      • 2014-01-31
      • 2019-10-23
      相关资源
      最近更新 更多