iOS Swift 从Popover视图控制器通过performSegue跳转时如何设置目标视图为全屏
Popover触发Segue跳转无法全屏的配置方案
问题描述
从Popover承载的视图控制器出发,通过Segue跳转新页面的传值逻辑已全部实现,但实际运行时,跳转后的评论页始终沿用Popover的浮层展示形式,无法以全屏模式加载。
现有实现代码
@IBAction func AddCommentBTN(_ sender: Any) { performSegue(withIdentifier: "comment", sender: self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "comment" { let destination = segue.destination as! CommentViewController destination.comments = self.comments destination.myId = self.myId } }
问题根因
- 很多开发者都会在这里踩坑:哪怕你在Storyboard的Segue配置面板里,已经把Present Modally的展示样式选为了Full Screen,只要触发跳转的源控制器是Popover,运行时跳转出来的新页面还是会变成浮层。
- 这是因为系统默认的模态样式继承机制,会把源控制器的Popover属性直接套到新的目标视图控制器上,完全覆盖你在Storyboard里手动设置的全屏规则。
具体修改步骤
- 不需要改动原有跳转触发、参数传递的逻辑,只需要在准备跳转、拿到目标控制器实例的代码块中,手动强制覆盖目标VC的展示样式为全屏即可:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "comment" { let destination = segue.destination as! CommentViewController // 单独指定目标页的展示样式,覆盖系统默认的浮层继承规则 destination.modalPresentationStyle = .fullScreen destination.comments = self.comments destination.myId = self.myId } }
注意:不要为了让目标页全屏,直接修改当前Popover本身的modalPresentationStyle参数,这种操作会直接破坏原有Popover的浮层弹出、交互逻辑。
常见踩坑提示:如果设置完全屏后,跳转返回时发现原Popover的位置出现偏移,只需要在Popover所在的父视图控制器的
viewWillAppear方法里补充一次布局刷新即可解决。
内容的提问来源于stack exchange,提问作者Jacob
相关产品推荐
相关产品推荐

