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

开发Carplay UI时界面与左侧侧边栏重叠问题求助

CarPlay界面与左侧侧边栏重叠问题

开发CarPlay UI时,运行出现界面与左侧侧边栏重叠的问题,相关代码及截图如下:

问题代码

UIWindowScene windowScene = new UIWindowScene(session, connectionOptions);

var ContainerViewController = UtilityIOS.CPStoryboard.InstantiateViewController("ContainerVC") as ContainerVC;
var registerController = UtilityIOS.CPStoryboard.InstantiateViewController("CarplayNavigationDashboardVC") as UINavigationController;

ContainerViewController.AddChildViewController(registerController);
registerController.View.Frame = new CGRect(0, 0, ContainerViewController.View.Frame.Width, ContainerViewController.View.Frame.Height);

ContainerViewController.View.AddSubview(registerController.View);
registerController.DidMoveToParentViewController(ContainerViewController);

this.Window = new UIWindow(windowScene);
// this.Window.Frame = new CGRect(40, 0, UIScreen.MainScreen.Bounds.Width - 40, UIScreen.MainScreen.Bounds.Height);
this.Window.RootViewController = ContainerViewController;
this.Window.MakeKeyAndVisible();

界面重叠截图

界面重叠截图

问题原因及解决方案

问题出在手动设置子视图Frame时,未考虑CarPlay界面的安全区域偏移——左侧侧边栏属于系统预留交互区域,直接从(0,0)开始布局会导致内容被覆盖。

方案1:用Auto Layout替代手动Frame设置

删除手动设置registerController.View.Frame的代码,通过Auto Layout约束让子视图自动适配父视图安全区域:

UIWindowScene windowScene = new UIWindowScene(session, connectionOptions);

var ContainerViewController = UtilityIOS.CPStoryboard.InstantiateViewController("ContainerVC") as ContainerVC;
var registerController = UtilityIOS.CPStoryboard.InstantiateViewController("CarplayNavigationDashboardVC") as UINavigationController;

ContainerViewController.AddChildViewController(registerController);
// 禁用自动转换约束,启用自定义Auto Layout
registerController.View.TranslatesAutoresizingMaskIntoConstraints = false;
ContainerViewController.View.AddSubview(registerController.View);
// 添加四边约束,绑定到父视图边缘
NSLayoutConstraint.ActivateConstraints(new[] {
    registerController.View.TopAnchor.ConstraintEqualTo(ContainerViewController.View.TopAnchor),
    registerController.View.LeadingAnchor.ConstraintEqualTo(ContainerViewController.View.LeadingAnchor),
    registerController.View.TrailingAnchor.ConstraintEqualTo(ContainerViewController.View.TrailingAnchor),
    registerController.View.BottomAnchor.ConstraintEqualTo(ContainerViewController.View.BottomAnchor)
});
registerController.DidMoveToParentViewController(ContainerViewController);

this.Window = new UIWindow(windowScene);
this.Window.RootViewController = ContainerViewController;
this.Window.MakeKeyAndVisible();

方案2:基于安全区域调整Frame

如果坚持使用Frame布局,需参考父视图的安全区域Insets修正布局起始位置:

// 替换原Frame设置代码
var safeAreaInsets = ContainerViewController.View.SafeAreaInsets;
registerController.View.Frame = new CGRect(
    safeAreaInsets.Left,
    safeAreaInsets.Top,
    ContainerViewController.View.Frame.Width - safeAreaInsets.Left - safeAreaInsets.Right,
    ContainerViewController.View.Frame.Height - safeAreaInsets.Top - safeAreaInsets.Bottom
);

额外注意

CarPlay的窗口尺寸由系统统一管理,无需手动调整this.Window.Frame,保留系统默认设置即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 17:20:27