在UINavigationController中使用sheetPresentationController呈现UIViewController

问题描述 投票:0回答:1

我有一个导航控制器,它有一个根视图控制器。我想使用sheetPresentationController 来呈现一个工作表。然后我想将另一个视图控制器推送到导航控制器。但该工作表仍保留在屏幕顶部。当我调试时,我看到该工作表是由导航控制器本身呈现的,而不是根视图控制器。我想要的是将工作表保留在根视图控制器的框架内。

这是我的代码和下面的 gif。

class ViewController: UIViewController {

    let sheet = UIViewController()
    let push = UIViewController()
    let child = UIViewController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
        
        let btnPush = UIButton()
        btnPush.setTitle("PUSH", for: .normal)
        btnPush.setTitleColor(.black, for: .normal)
        btnPush.addTarget(self, action: #selector(btnPushPressed), for: .touchUpInside)
        
        child.view.addSubview(btnPush)
        
        child.view.translatesAutoresizingMaskIntoConstraints = false
        btnPush.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            child.view.topAnchor.constraint(equalTo: view.topAnchor),
            child.view.leftAnchor.constraint(equalTo: view.leftAnchor),
            child.view.rightAnchor.constraint(equalTo: view.rightAnchor),
            child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            btnPush.centerXAnchor.constraint(equalTo: child.view.centerXAnchor),
            btnPush.centerYAnchor.constraint(equalTo: child.view.centerYAnchor, constant: -200),
        ])

        sheet.sheetPresentationController?.detents = [.medium()]
        sheet.sheetPresentationController?.largestUndimmedDetentIdentifier = .medium
        
        sheet.view.backgroundColor = .red
        push.view.backgroundColor = .white
        
        child.present(sheet, animated: true)
    }

    @objc
    func btnPushPressed() {
        navigationController?.pushViewController(push, animated: true)
    }
}

swift uikit uinavigationcontroller uisheetpresentationcontroller
1个回答
0
投票

您可以在

viewWillDisappear
方法中添加逻辑来关闭所显示的工作表。

override func viewWillDisappear(_ animated: Bool) {      
   super.viewWillDisappear(animated)
   
   // Dissmiss here.
   sheet.dismiss(animated: true)
}

我不太确定

When I debug, I see that the sheet is presented by navigation controller itself, instead of the root view controller.
.

我认为它应该由

child
视图控制器呈现,这就是你呈现表格的地方。我希望有人可以对这个问题有想法!

© www.soinside.com 2019 - 2024. All rights reserved.