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

如何将UITabController控制的UIViewController改为底部滑入式弹窗?

Swift实现底部滑入的模态弹窗(禁止TabBar交互+背景暗化)

一、Storyboard基础设置(修正之前的配置问题)

  • 打开Storyboard,选中VC4,在右侧Attributes Inspector面板:
    • 将Presentation改为Over Full Screen(这是核心,默认的Full Screen会替换当前页面,无法保留下方VC的暗化效果)
    • 将Transition改为Cover Vertical(原生底部滑入动画)
  • 在VC4的根View上添加中间对话框容器(比如命名为dialogView),把VC4的所有内容放入该容器:
    • 根View设置为半透明黑色(如alpha 0.6),实现周围区域暗化
    • dialogView设置为不透明底色(如白色),调整到屏幕中间,可添加圆角、阴影等样式

二、弹出VC4的代码实现(任意VC均可调用)

在需要弹出VC4的位置,通过模态present方式调用,同时禁用TabBar交互:

// 从Storyboard获取VC4实例(需提前给VC4设置Storyboard ID,比如"VC4ID")
guard let vc4 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC4ID") as? VC4ViewController else { return }
// 确认模态样式(Storyboard已设置的话可省略,写代码更保险)
vc4.modalPresentationStyle = .overFullScreen
vc4.modalTransitionStyle = .coverVertical
// 弹出前禁用TabBar点击
self.tabBarController?.tabBar.isUserInteractionEnabled = false
// 弹出弹窗
self.present(vc4, animated: true, completion: nil)

三、关闭弹窗时恢复TabBar交互

在VC4的关闭按钮点击事件中,关闭弹窗后恢复TabBar的可交互性:

@IBAction func closeButtonTapped(_ sender: UIButton) {
    self.dismiss(animated: true) {
        // 回到原页面后恢复TabBar功能
        self.presentingViewController?.tabBarController?.tabBar.isUserInteractionEnabled = true
    }
}

四、之前修改无效的原因分析

大概率是Presentation未设置为Over Full Screen:默认的Full Screen会完全替换当前显示的VC,既看不到下方页面的暗化效果,TabBar也会被隐藏,自然无法实现你要的弹窗效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 23:15:19