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.
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.
Init: Create two
UIViewController
in storyboard.- lets say
FirstViewController
which is normal andSecondViewController
which will be the popup.
- lets say
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
UIView
and make any kind of design you want to show. Here is a screenshot of my storyboard- Here I have add segue on login button which open
SecondViewController
as popup to ask username and password
- Here I have add segue on login button which open
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 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
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:
And the result:
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.
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
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
Add "...Read More" to the End of Uilabel
Get Indexpath of Uitableviewcell on Click of Button from Cell
Changing Specific Text's Color Using Nsmutableattributedstring in Swift
How to Use Uiview Autoresizingmask Property Programmatically
Cordova App Not Displaying Correctly on Iphone X (Simulator)
Trying to Convert Firebase Timestamp to Nsdate in Swift
How to Make App Fully Working Correctly for Autorotation in iOS 6
Change Color of Certain Pixels in a Uiimage
How to Hide the Status Bar in a Swift iOS App
Why Is Wkwebview Not Opening Links With Target="_Blank"
How to Launch Safari and Open Url from iOS App
Wkwebview Equivalent for Uiwebview's Scalespagetofit
Uipageviewcontroller and Storyboard
Cordova App Hanging During Startup on iOS 10
Ld: File Not Found: Linker Command Failed with Exit Code 1
Enabling Auto Layout in iOS 6 While Remaining Backwards Compatible With iOS 5