swift - 添加 subview 并置于前面,但仍然不显示

标签 swift xcode uiview uiviewcontroller

我正在尝试创建一个简单的 Xcode 应用程序,按下按钮即可滑出侧面板。我知道有很多教程,但由于各种原因,我没有找到任何可以满足我特别需要的教程。 我尝试采取的第一步是在按下按钮时弹出一个新 View (完全位于我的第一个 VC 之上),但我遇到了问题。

按下按钮时将调用以下函数:

let otherVC = OtherViewController()
self.view.insertSubview(otherVC.view, at: 0)
self.view.bringSubviewToFront(otherVC.view)

OtherViewController 是在 Storyboard 中设置的,我知道它的 viewDidLoad 函数是用上面的代码调用的......我只是不明白为什么完全没有任何反应,并且另一个 VC 没有出现。我还应该做些什么来达到我想要的结果吗?

最佳答案

您不能像任何其他对象一样通过启动来调用 View Controller , Storyboard中的 View 不会附加到它

相反,

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) 
let otherVC = storyboard.instantiateViewController(withIdentifier: "OtherViewController") as! OtherViewController

除此之外,您还必须为 StoryBoard 中的 viewController 分配一个 Storyboard Id。

You have to add the Storyboard ID here

之后,您应该查看 UIView Containment 以获得进一步的帮助

关于swift - 添加 subview 并置于前面,但仍然不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554308/

相关文章:

ios - 屏幕左侧的 UIButton(在 iOS 7 导航滑动区域)未突出显示

ios - 使用 NSDate 的核心数据排序描述符,使用 Swift 的 iOS

ios - 显示 UIPickerView 时如何向上移动 View

ios - 移动 View 并更改 uiimageview 的图像不执行 View 的移动

ios - UIView 中的 UIButton 在另外两个 UIView 后面对上面的 View 的触摸使用react

ios - Xcode:找不到体系结构 armv7 的符号

swift - 如何在放大 MKMapView 时更改自定义图钉图像?

ios - 单击单元格时如何根据排序移动单元格?

swift - Xcode 9.4 Playground Swift 控制台输出

xcode - 查看导致 pthread_mutex_lock 死锁的原因