Embedding subview in main view Swift

半腔热情 提交于 2019-12-11 07:57:14

问题


If you look at this image (I don't want to upload it, as it does not belong to me), you will see what appears to be a uiview inside the main uiview controller in the settings app of an iPad. My question is, how do I replicate this programatically? In other words, how do I embed a UIView in another?

Here's what I know and have done:

  • Based on my research, this is called "subviews". Is this correct?
  • I have created a UIView with the proper elements that I want in interface builder. It is currently not a subview, but at the same level hierarchically as my main view.
  • I found the same exact question except in Obj-C, not my language (Swift).

Here's what I need:

  • How do I programatically spawn a subview in swift once a button is clicked?
  • As this subview is pretty complex in terms of UIelements, I want to be able to design it in interface builder.

Here's what I have so far:

 @IBAction func buttonPressedSpawnSubview(sender: AnyObject) {
    //Open a subview from Interface builder.
}
@IBAction func closeButtonPressedSpawnSubview(sender: AnyObject) {
        //kill the subview.

Can someone help me figure out how fill in the commented lines?


回答1:


What you are seeing is a modal UIView on top of another view.

An example from within your visible ViewController would be:

    self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal // Choose whatever transition you want
    self.modalPresentationStyle = .CurrentContext // Display on top of current UIView
    self.presentViewController(yourNewViewObject, animated: true, completion: nil)


来源:https://stackoverflow.com/questions/25141983/embedding-subview-in-main-view-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!