【问题标题】:Defer system edge gestures in only some view controllers. SwiftUI仅在某些视图控制器中延迟系统边缘手势。 SwiftUI
【发布时间】:2021-08-01 02:11:03
【问题描述】:

我想导航到禁用系统边缘手势的自定义 UIView。我正在使用带有 UIViewControllerRepresentable 的 SwiftUI 生命周期并覆盖 PreferredScreenEdgesDeferringSystemGestures。

我已经看到了 SceneDelegate 的解决方案。 preferredScreenEdgesDeferringSystemGestures 是否必须作用于 window.rootViewController 才能工作?

class MyUIViewController: UIViewController {
    typealias UIViewControllerType = MyUIViewController
    open override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge {
        return [.all];
    }
    let labelDescription: UILabel = {
        let label = UILabel()
        label.text = "But it's not working."
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(labelDescription)
        labelDescription.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
        setNeedsUpdateOfScreenEdgesDeferringSystemGestures()
    }
}
struct UIViewControllerRepresentation : UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        let uiViewController = MyUIViewController()
        return uiViewController
    }
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}
struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink("To UIView with no system edge gestures.",
                           destination: UIViewControllerRepresentation())
                .navigationTitle("Title")
        }
    }
}

【问题讨论】:

    标签: ios swift gesture uiviewcontrollerrepresentable


    【解决方案1】:

    https://developer.apple.com/documentation/uikit/uiviewcontroller/2887511-childforscreenedgesdeferringsyst

    如果我理解正确,系统只会询问第一个 UIViewController,如果该 vc 没有返回系统也应该询问的子项,那么就是这样。

    由于您无权访问 SwiftUI 中的视图控制器(甚至不知道它将使用哪些类型的视图控制器),我选择只调整 childForScreenEdgesDeferringSystemGestureschildForHomeIndicatorAutoHidden getter 并返回管理视图控制器这些是通过循环遍历所有 UIViewController.children 为我提供的。

    由于您从我的 Gist 链接到这个问题,我将链接回那里以获得特定于我的 Gist 的解决方案。 https://gist.github.com/Amzd/01e1f69ecbc4c82c8586dcd292b1d30d

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      相关资源
      最近更新 更多