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

iOS开发:新弹出的ViewController中Navigation Bar不显示如何解决?

问题原因

你直接通过present方式弹出的makeTaskViewController没有嵌套在UINavigationController容器中,只有被UINavigationController管理的控制器才会自动渲染导航栏,你给vc设置的navigationItem仅在它处于导航栈内时才会生效。

解决方法

可根据你的交互需求任选一种方案:

方案1:弹出时给目标控制器套一层导航控制器

如果需要保留全屏模态弹出的效果,将目标控制器包装在UINavigationController中再弹出即可:

@objc func didTapPlus(){
    let vc = storyboard?.instantiateViewController(identifier: "makeTask") as! makeTaskViewController
    // 给目标控制器套上导航容器
    let navVc = UINavigationController(rootViewController: vc)
    navVc.modalPresentationStyle = .fullScreen
    
    vc.title = "Create a Task"
    vc.view.backgroundColor = .black
    
    vc.navigationItem.leftBarButtonItem = UIBarButtonItem(
        title: "Back",
        style: .done,
        target: self,
        action: #selector(backFromCreateTask))
    
    present(navVc, animated: true)
}

// 补充返回按钮点击逻辑
@objc func backFromCreateTask() {
    dismiss(animated: true)
}

方案2:用push方法代替present弹出

如果不需要模态弹出的交互特性,直接使用当前页面所属导航栈的push方法跳转,默认自带导航栏、系统返回按钮和侧滑返回手势:

@objc func didTapPlus(){
    let vc = storyboard?.instantiateViewController(identifier: "makeTask") as! makeTaskViewController
    vc.title = "Create a Task"
    vc.view.backgroundColor = .black
    // 走导航栈push逻辑,无需额外配置导航栏
    navigationController?.pushViewController(vc, animated: true)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 17:24:08