Embedding subview in main view Swift
Asked Answered
S

1

1

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?

Slattery answered 5/8, 2014 at 14:55 Comment(0)
G
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)
Gerta answered 5/8, 2014 at 15:9 Comment(8)
Now how would I make this code pull up the view I designed in interface builder when the code executes? And how do I kill the modal view? Sorry, I'm a newbie at programming.Slattery
Do you have a viewcontroller created for the popup?Gerta
You can dismiss the modal viewcontroller using it's own IBAction (since it's modal, it will not allow you to interact with the interface behind it) using: self.dismissModalViewControllerAnimated(true)Gerta
I have created a view controller w/ all the content in inter face builderSlattery
You'd create yourNewViewObject with something like: var yourNewViewObject : SecondViewController = SecondViewController() and then just pass it to presentViewController as above.Gerta
one more question (sorry!)Can all the above code be done in the main view controller.swift file, or do I have to make a different viewcontroller.swift file?Slattery
The code to display the modal, or to dismiss it? The code to display it can be located in any view controller at all (main or not). It just has to be present in any view controller that is currently visible, else it won't be able to invoke the modal.Gerta
I re-asked the question to make it clearer [link] #25224107 [/link] mind taking a look?Slattery

© 2022 - 2024 — McMap. All rights reserved.