我使用 UIPresentationController
来显示底部提示。有时候 presentationController
可能会呈现另一个控制器。当呈现的控制器被解除时,presentationController
的高度会发生变化。为什么会发生变化,我该如何解决这个问题。代码如下:
class ViewController: UIViewController {
let presentVC = UIViewController()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
guard let `self` = self else { return }
self.presentBottom(self.presentVC)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
guard let `self` = self else { return }
let VC = UIViewController()
VC.view.backgroundColor = .red
self.presentVC.present(VC, animated: true, completion: {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
VC.dismiss(animated: true, completion: nil)
}
})
}
}
}
public class PresentBottom: UIPresentationController {
public override var frameOfPresentedViewInContainerView: CGRect {
let bottomViewHeight: CGFloat = 200.0
let screenBounds = UIScreen.main.bounds
return CGRect(x: 0, y: screenBounds.height - bottomViewHeight, width: screenBounds.width, height: bottomViewHeight)
}
}
extension UIViewController: UIViewControllerTransitioningDelegate {
public func presentBottom(_ vc: UIViewController ) {
vc.view.backgroundColor = .purple
vc.modalPresentationStyle = .custom
vc.transitioningDelegate = self
self.present(vc, animated: true, completion: nil)
}
public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
let vc = PresentBottom(presentedViewController: presented, presenting: presenting)
return vc
}
}
我的代码如下:
![输入图像描述](https://istack.dev59.com/691M1.webp)
presentationController
的高度如下图所示:
让我感到困惑的是 present 视图控制器的高度发生了改变: