Display Uiviewcontroller as Popup in Iphone

Popup UIViewController

To make your view controller shown as a popup, you should set the following:

popupVC.modalPresentationStyle = .OverCurrentContext
popupVC.modalTransitionStyle = .CrossDissolve

You should also design your view controller's position, size to make it look like a popup.

Here is my popup that i did before.

Sample Image

Display UIViewController as Popup in iPhone

NOTE : This solution is broken in iOS 8. I will post new solution ASAP.

I am going to answer here using storyboard but it is also possible without storyboard.

  1. Init: Create two UIViewController in storyboard.

    • lets say FirstViewController which is normal and SecondViewController which will be the popup.

  2. Modal Segue: Put UIButton in FirstViewController and create a segue on this UIButton to SecondViewController as modal segue.

  3. Make Transparent: Now select UIView (UIView Which is created by default with UIViewController) of SecondViewController and change its background color to clear color.

  4. Make background Dim: Add an UIImageView in SecondViewController which covers whole screen and sets its image to some dimmed semi transparent image. You can get a sample from here : UIAlertView Background Image

  5. Display Design: Now add an UIView and make any kind of design you want to show. Here is a screenshot of my storyboard
    storyboard

    • Here I have add segue on login button which open SecondViewController as popup to ask username and password
  6. Important: Now that main step. We want that SecondViewController doesn't hide FirstViewController completely. We have set clear color but this is not enough. By default it adds black behind model presentation so we have to add one line of code in viewDidLoad of FirstViewController. You can add it at another place also but it should run before segue.

    [self setModalPresentationStyle:UIModalPresentationCurrentContext];

  7. Dismiss: When to dismiss depends on your use case. This is a modal presentation so to dismiss we do what we do for modal presentation:

    [self dismissViewControllerAnimated:YES completion:Nil];

Thats all.....

Any kind of suggestion and comment are welcome.

Demo :
You can get demo source project from Here : Popup Demo

NEW : Someone have done very nice job on this concept : MZFormSheetController

New : I found one more code to get this kind of function : KLCPopup


iOS 8 Update : I made this method to work with both iOS 7 and iOS 8

+ (void)setPresentationStyleForSelfController:(UIViewController *)selfController presentingController:(UIViewController *)presentingController
{
if (iOSVersion >= 8.0)
{
presentingController.providesPresentationContextTransitionStyle = YES;
presentingController.definesPresentationContext = YES;

[presentingController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
}
else
{
[selfController setModalPresentationStyle:UIModalPresentationCurrentContext];
[selfController.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];
}
}

Can use this method inside prepareForSegue deligate like this

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

PopUpViewController *popup = segue.destinationViewController;
[self setPresentationStyleForSelfController:self presentingController:popup]
}

iOS Present View Controller Within Popup

This is only with Storyboard.

1) Create an UIViewController (blue) and ctrl + drag (mouse) from your UIBarButtonItem to the UIViewController and select "present as Popover" (like you did).

2) Click on the UIViewController (blue) and click on Editor->embed in->Navigation Controller (this will be trick to let the next controller stay in the popup)

3) Create a second UIViewController (green)

4) Create a UIButton in the first UIViewController (blue) and ctrl + drag from the button to the second UIViewController (green) and select "show"

At the end it should look like this in Storyboard:

Sample Image

And the result:

Sample Image

Sample Image

If you want the PopOver without the navigationBar you can use in the blue controller:

self.navigationController?.isNavigationBarHidden = true

and to go back from the green to the blue view you can use in the green controller:

@IBAction func backToBlueController(sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}

Additional:

If you don't want to use the popUp you could also change the segue kind from the barButtonItem to the navigationController to

Present Modally

and the presentation to something like

Form Sheet

in Storyboard.

Sample Image

At a glance, you should always use an UINavigationController to manage your navigation, even if you don't need the navigationBar, because the navigation controller provides you a navigation stack from where you can pop and push into it.

UINavigationController Reference

Display UIViewController as Popup without seguge

You should check if this VC self.navigationController is not nil. and i think you want to presentViewController: not pushViewController:...

Programmatically showing another UIViewController as popup in iOS?

popupObj= [self.storyboard instantiateViewControllerWithIdentifier:@"popup"];
popupObj.view.frame = CGRectMake(20, 200, 280, 168);
[self.view addSubview:popupObj.view];
[self addChildViewController:popupObj];

You can add UIViewController as subview and set it's frame so it will look like popup.
Hope this will help you.

Presenting a popup view controller programatically using auto layout

If you are not adding constraints yourself - which, in this case, you are not - you still need to tell the view how to behave.

Change your createSubviews() function to this:

func createSubViews() {
//translatesAutoresizingMaskIntoConstraints = false

autoresizingMask = [.flexibleHeight, .flexibleWidth]

backgroundColor = .yellow

addSubview(blueBox)

blueBox.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
blueBox.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
blueBox.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.7).isActive = true
blueBox.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.4).isActive = true

}

That will allow auto-layout to re-layout your view(s) on device rotation.

iOS - UIViewController as Popup with dynamic height

Imagine the following UI

Sample Image

a centered UIView that contains a UIButton and UITableView , you need to hook height constraint of the table and do this inside the popup if you want to hide it

@IBOutlet weak var heightTblCon:NSLayoutConstraint!

//

self.heightTblCon.constant = show ? 300 : 0
self.view.layoutIfNeeded()

BTW i changed color of background view for clarification purposes that should be transparent for modals



Related Topics



Leave a reply



Submit