You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中如何为present的MapBox NavigationViewController设置底部约束

Hey there! I’ve run into a similar issue with presenting view controllers and needing custom frame adjustments, so let’s get your Mapbox NavigationViewController sitting with that 300pt bottom space.

The problem with setting constraints directly on the presented NavigationViewController is that when you present a view controller with the default .fullScreen modal style, it takes over the entire screen and you can’t modify its constraints directly from the presenting side. Instead, we can use a custom presentation controller to define exactly how the presented view should be framed.

Here’s how to implement it step by step:

  1. First, create a custom UIPresentationController subclass that will handle the frame calculation for your NavigationViewController:
class BottomInsetPresentationController: UIPresentationController {
    override var frameOfPresentedViewInContainerView: CGRect {
        guard let container = containerView else { return super.frameOfPresentedViewInContainerView }
        // Define your desired bottom inset here
        let bottomSpacing: CGFloat = 300
        return CGRect(
            x: 0,
            y: 0,
            width: container.bounds.width,
            height: container.bounds.height - bottomSpacing
        )
    }
}
  1. Make your presenting view controller conform to UIViewControllerTransitioningDelegate so it can supply the custom presentation controller:
extension YourPresentingViewController: UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return BottomInsetPresentationController(presentedViewController: presented, presenting: presenting)
    }
}

(Replace YourPresentingViewController with the actual name of the controller where you’re calling presentAndRemoveMapview.)

  1. Update your present method to use the custom presentation style and set the transitioning delegate:
func presentAndRemoveMapview(_ navigationViewController: NavigationViewController) {
    // Set custom presentation style and delegate
    navigationViewController.modalPresentationStyle = .custom
    navigationViewController.transitioningDelegate = self
    
    present(navigationViewController, animated: true) {
        self.mapView?.removeFromSuperview()
        self.mapView = nil
    }
}

This works because the custom presentation controller overrides the default frame of the presented view, forcing it to leave that 300pt gap at the bottom. Since you’re still using the present method as you preferred, this keeps your workflow intact without having to switch to embedding the NavigationViewController’s view directly.

One quick optional tweak: if you want a dimming background behind the presented NavigationViewController (like the default present animation), you can override presentationTransitionWillBegin in your custom presentation controller to add a dimmed view to the container. But that’s totally up to your UI needs.

内容的提问来源于stack exchange,提问作者Tony Hensler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:52:36