Embedding subview in main view Swift

2019-08-06 01:52发布

问题:

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)