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?
Imao put UIImageView on background is not the best idea . In my case i added on controller view other 2 views . First view has
[UIColor clearColor]
on background, second - color which u want to be transparent (grey in my case).Note that order is important.Then for second view set alpha 0.5(alpha >=0 <=1).Added this to lines inprepareForSegue
And thats all.
Modal Popups in Interface Builder (Storyboards)
Step 1
On the ViewController you want as your modal popup, make the background color of the root UIView clear.
Tip: Do not use the root UIView as your popup. Add a new UIView that is smaller to be your popup.
Step 2
Create a Segue to the ViewController that has your popup. Select "Present Modally".![Segue](https://i.stack.imgur.com/Lc5Y3.png)
Two Methods To Create Popup From Here
Method One - Using the Segue
Select the Segue and change Presentation to "Over Current Context":![Over Current Context](https://i.stack.imgur.com/le0tK.png)
Method Two - Using the View Controller
Select the ViewController Scene that is your popup. In Attributes Inspector, under View Controller section, set Presentation to "Over Current Context":![ViewController](https://i.stack.imgur.com/qjq7J.png)
Either method will work. That should do it!
You can do this to add any other subview to the view controller. First set the status bar to None for the ViewController which you want to add as subview so that you can resize to whatever you want. Then create a button in Present View controller and a method for button click. In the method:
Hope this helps, feel free to ask if any queries...
You can do this in Interface Builder.
You can use EzPopup (https://github.com/huynguyencong/EzPopup), it is a Swift pod and very easy to use:
Feel free to use my form sheet controller MZFormSheetControllerfor iPhone, in example project there are many examples on how to present modal view controller which will not cover full window and has many presentation/transition styles.
You can also try newest version of MZFormSheetController which is called MZFormSheetPresentationController and have a lot of more features.