Since there is no complete, definitive answer to this common recurring question, I'll ask and answer it here.
Often we need to present a UIViewController
such that it doesn't cover full screen, as in the picture below.
Apple provides several similar UIViewController
, such as UIAlertView
, Twitter or Facebook share view controller, etc..
How can we achieve this effect for a custom controller?
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.
Init: Create two
UIViewController
in storyboard.FirstViewController
which is normal andSecondViewController
which will be the popup.Modal Segue: Put
UIButton
in FirstViewController and create a segue on thisUIButton
toSecondViewController
as modal segue.Make Transparent: Now select
UIView
(UIView
Which is created by default withUIViewController
) ofSecondViewController
and change its background color to clear color.Make background Dim: Add an
UIImageView
inSecondViewController
which covers whole screen and sets its image to some dimmed semi transparent image. You can get a sample from here :UIAlertView
Background ImageDisplay Design: Now add an![storyboard](https://i.stack.imgur.com/RLRef.png)
UIView
and make any kind of design you want to show. Here is a screenshot of my storyboardSecondViewController
as popup to ask username and passwordImportant: 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 ofFirstViewController
. You can add it at another place also but it should run before segue.[self setModalPresentationStyle:UIModalPresentationCurrentContext];
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
Can use this method inside prepareForSegue deligate like this
Swift 4:
To add an overlay, or the popup view You can also use the Container View with which you get a free View Controller ( you get the Container View from the usual object palette/library)
Steps:
Have a View (ViewForContainer in the pic) that holds this Container View, to dim it when the contents of Container View are displayed. Connect the outlet inside the first View Controller
Hide this View when 1st VC loads
Unhide when Button is clicked![enter image description here](https://i.stack.imgur.com/HpzFU.png)
To dim this View when the Container View content is displayed, set the Views Background to Black and opacity to 30%
You will get this effect when you click on the Button![enter image description here](https://i.stack.imgur.com/EXnNc.png)