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

Swift中如何调整voiceOverlay弹出的ViewController的展示尺寸?

报错问题修复

你遇到的报错是基础语法错误:代码中InputViewController.view调用时用的是InputViewController类名,类无法直接访问实例属性view,需要替换为你实际创建的inputViewController实例对象(注意首字母小写,为你之前实例化的控制器变量)。

修正后的相关代码如下:

view.addChild(inputViewController)
inputViewController.didMove(toParent: view)
// 将实例view作为参数传入约束设置方法
setContraintsForPresenting(childView: inputViewController.view)

fileprivate func setContraintsForPresenting(childView: UIView){
    childView.translatesAutoresizingMaskIntoConstraints = false
    // 在这里添加你需要的尺寸、位置约束,例如固定宽高300*200、居中显示
    NSLayoutConstraint.activate([
        childView.widthAnchor.constraint(equalToConstant: 300),
        childView.heightAnchor.constraint(equalToConstant: 200),
        childView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        childView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    ])
}

弹窗尺寸调整可选方案(无需改为addChild也可实现)

如果不想修改pod的present逻辑,也可以通过自定义模态样式实现固定尺寸+背景可交互:

  • 将原有的modalPresentationStyle = .overCurrentContext改为modalPresentationStyle = .custom
  • 设置inputViewController.preferredContentSize为你需要的固定尺寸
  • 实现UIViewControllerTransitioningDelegate协议的presentationController(forPresented:presenting:source:)方法,返回自定义的UIPresentationController,在其中设置弹窗的frame、透传背景点击事件即可实现背景区域可交互。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 21:36:00