I use WEPopover (https://github.com/werner77/WEPopover) to display popovers on iPhone. Unfortunately it does not work if I use a general UIViewController for the contentViewController of it.
- (IBAction)showPopover:(id)sender
{
UIViewController *contentViewController = [[PopoverContentViewController alloc] init];
popoverController = [[WEPopoverController alloc] initWithContentViewController:contentViewController];
popoverController.delegate = self;
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:(UIPopoverArrowDirectionUp) animated:YES];
}
This just presents me a black popover with no content. The PopoverContentViewController is just a normal UIViewController generated by XCode without any changes to it. In my storyboard I added a ViewController, set its size to freeform and set the view's size to something lower than standard iPhone size. I set the owner of this viewcontroller the PopoverContentViewController. Unfortunately it does not show the content, it just shows a black popup.
If I change the the PopoverContentViewController to derive from UITableViewController it shows a table view, but that is not what I want.
What have I done wrong?
You likely need to load your view controller from the storyboard instead of instantiating it with alloc/init as you show.
Something like this:
UIViewController* contentViewController = [[UIStoryboard storyboardWithName: #"yourStoryboardName" bundle: nil] instantiateViewControllerWithIdentifier: #"yourViewControllerID"];
Related
I would like to modally add a view controller with a transparent background, so the parent view controller beneath can be seen. (This is in an app for iPhone, not for iPad.)
I have tried this:
TextFieldViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"TextFieldVC"];
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController presentViewController:vc animated:YES completion:^{}];
Without luck and given the view controller a clear color background. My view controller is in a storyboard if that changes anything.
#Josh Kahane
set the view controller that will present the transparent view controller with this code at the -ViewDidLoad
and be sure to set the alpha channel of the UIViewController View to be lower then 1.
Code:
self.modalPresentationStyle = UIModalPresentationCurrentContext;
I have been searching for the solution. Now thanks to iOS 8. They has introduced couple of new modalPresentationStyle. one among them is UIModalPresentationOverCurrentContext. Used the same to solve the this issue.
viewcontroller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
Hope this helps.
For completeness and having it up-to-date, I am also adding the solution for Swift:
either
viewController.modalPresentationStyle = .CurrentContext
being - A presentation style where the content is displayed over only the presenting view controller’s content.
or
viewController.modalPresentationStyle = .OverCurrentContext
being - A presentation style where the content is displayed over only the parent view controller’s content. The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.
Depending on the requirements and circumstances for the presentation.
It's possible although there are a bunch of steps which can be easily forgotten. After struggling with myself to get this working for 4 hours I came up with the following solution.
1 - Create a View Controller like any other.
Header file
#import "DDBaseViewController.h"
#interface DDHomeSearchLoadingViewController : UIViewController
#end
Implementation file
#import "DDHomeSearchLoadingViewController.h"
#interface DDHomeSearchLoadingViewController ()
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityMonitor;
#property (weak, nonatomic) IBOutlet UIView *modalView;
#end
#implementation DDHomeSearchLoadingViewController
#pragma mark - UIViewController lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
}
-(void) setupUI
{
[self makeRoundedCorner:self.modalView AndCornerRadius:6.0f];
[self.activityMonitor startAnimating];
}
-(void) makeRoundedCorner:(UIView*) view AndCornerRadius:(float) cornerRadius
{
[view.layer setCornerRadius:cornerRadius];
[view.layer setMasksToBounds:YES];
}
#end
2 - Set your container view background color as ClearColor
3 - Add a UIView which will be presented like an overlay
4 - Add a UIView which will be presented like a Dialog Box over the overlay UIView
Make sure it's outside the overlay view when you add/move it. For some reason when you move it using the mouse it adds to overlay UIView automatically. ( Freaking annoying to be honest )
5 - Set the Storyboard ID for the View you've created
6 - Finally add this piece of code wherever you wan't call it ( Assuming it's a UiViewController too )
DDHomeSearchLoadingViewController* ddHomeSearchLoadingViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"DDHomeSearchLoadingViewController"];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:ddHomeSearchLoadingViewController animated:YES completion:nil];
I hope it helps you guys out
Cheers
Answer for Swift 5.
TransparentViewController
self.view.backgroundColor = .clear
PresentingViewController
let viewController = TransparentViewController()
viewController.modalPresentationStyle = .overFullScreen
navigationController.present(viewController, animated: false)
Swift 3 -
Try vc.modalPresentationStyle = .overFullScreen
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("ExampleViewController") as! ExampleViewController
vc.view.backgroundColor = UIColor.clearColor()
vc.modalPresentationStyle = .overFullScreen
self.presentViewController(vc, animated: true, completion: nil)
you can make changes in storyboard just by doing below options and also reducing parental view opacity. Using Storyboard: No need to write any code to achieve this
UIStoryboard *story = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ChooseController *sec = [story instantiateViewControllerWithIdentifier:#"Controller"];
sec.modalPresentationStyle = UIModalPresentationOverCurrentContext;
sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:sec animated:YES completion:^{}];
Note: Present controller superview alpha value must be below 1 like 0.5 alpha like that.
When you modally present a view controller the , it goes on the stack which in turn hides the view controller below it. So all you can do is to present a transparent view with animation similar to modally presenting a view controller.
I try this:
ViewController.h
#class SecondView;
#interface Introduccion : UIViewController{
SecondView *second;
}
-(IBAction)AnimatecreditsPage:(id)sender;
#end
ViewController.m
-(IBAction)AnimatecreditsPage:(id)sender{
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
second.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:second animated:YES];
}
Im using Storyboards, and i already linked my viewcontroller with the corresponding classes, when i press the button, the iphone simulator just crash.. im using a navigation controller and tab bar controller.
Image of the viewcontroller
THANKS!! :)
Please Help Me.
The way you're trying is crashing because you're pointing to a xib that doesn't exist. Since with storyboards you can have multiple view controllers you have to add an identifier to the view controller you wish to use in the attributes inspector section of interface builder. This then allows you to use the following code to programmatically instantiate what ever view controller in your storyboard has the ID you specify.
second = [self.storyboard instantiateViewControllerWithIdentifier:#"someID"];
Instead of:
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
I would like to have a UISegmentedControl embedded in a PopoverController, similar to what is described in this SO question : UISegmentedControl embedded in a UINavigationBar/Item
The difference is that I have a different view controller for each view that I want to show in the popover, depending on the selected index on the Segmented Control. I'm not sure how I would go about doing this. Whenever I try to push a new view on top of the root view controller, the UISegmentedControl disappears. I would just like to switch between the two viewcontrollers, while keeping the UISegmentedControl visible. Is this even possible?
Thanks in advance!
If its a different viewController for each one of the segments on the segmentBar, you'll have to use a container viewController that adds the views of each of the viewController as a subview on itself or sets its view to that of the viewController's view. For example:
UIViewController* containerController = [[[UIViewController alloc] init] autorelease];
//Inside the viewDidLoad of the the ContainerController class, do the following:
//Initialize all three viewControllers
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
//set up the segment and add it to the container's navBar's title view.
[segmentedControl addTarget:self action:#selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
- (void)segmentValueChanged:(id)sender
{
//if first tab selected
[self.view removeAllSubviews];
[self.view addSubview:test1.view];
//if second tab selected
[self.view removeAllSubviews];
[self.view addSubview:test2.view];
//if third tab selected
[self.view removeAllSubviews];
[self.view addSubview:test3.view];
}
Instead of adding it as a subView, you might be able to just set self.view = test1.view. Obviously, you would use the container view to initialize the navController and put that navController inside the popover. Hope this helps!
If you are using presentModalViewController method to show your new view controller on the screen, it will always cover the entire screen and what ever is underneath it. That's just how it works.
As per docs:
On iPhone and iPod touch devices, the view of modalViewController is
always presented full screen. On iPad, the presentation depends on the
value in the modalPresentationStyle property.
The way to do it and still being able to control how the view controller is positioned is to create your own presentation method.
I have a View application with a Single UIViewController. I then add a UITableViewController through the IB, and I am trying to display the UITableViewController through a button press in the UIViewController (my main view). My button press (IBAction) contains the following code through which I am trying to push my UITableViewController view and display it:
DataViewController *dataController = [[DataViewController alloc] initWithNibName: #"DataViewController" bundle:nil];
[self.navigationController pushViewController:dataController animated:YES];
[dataController release];
My DataViewController is not at all getting pushed into the stack and displayed,
Also I have checked that in the code above, self.navigationController=nil
Probably this is the source of the problem. If so, how to rectify it?
Please help.
UINavigationController *navCtrlr = [[UINavigationController alloc]initWithRootViewController:yourfirstviewController];
[self.window setRootViewController:navCtrlr];
navCtrlr.delegate = self;
navCtrlr.navigationBarHidden = YES;
Create navigation controller in appdelegate.m then you can navigate to any uiviewcontroller
You need to actually create a UINavigationController. The navigationController property tells you whether your DataViewController is currently in a UINavigationController's hierarchy; if not (as in this case), the navigationController property returns nil.
I have a subclass of TTMessageController that shows ... BUT it is not animated even though it should be. The code that displays the modal view looks like this (where PostToWebMessageController is the subclass of TTMessageController:
if (self.toWebMsgController == nil) {
self.toWebMsgController = [[PostToWebMessageController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:self.toWebMsgController animated:NO];
[self presentModalViewController:navController animated:YES];
What happens though is this: The screen goes black ... the keyboard scrolls up into view ... and THEN the TTMessageController view shows up (not animated). When I dismiss the view via a Cancel button the screen goes black and then just disappears (no animation again).
Any ideas why this is happening? I've this with a number of other TT* controllers and I can't get one to animate right with showing modally.
Thanks
UPDATE:
This is happening in EVERY UIViewController that I try to present modally. Screen goes black, keyboard animates upwards and then view displays. Any ideas why this might be happening???
A day to figure this out ... hopefully someone will benefit from my pains!
Here is what is happening:
The UIViewController calling presentModalViewController is itself nested inside a UIScrollView that is contained in ANOTHER UIViewController. Apparently, cocoa touch doesn't much like this. Anyhow, to rectify the problem I did the following:
Add a property of type UIViewController to the UIViewController that will present a modal view controller (e.g. #property (nonatomic, retain) UIViewController *owningController;)
Set that property = to the topmost UIViewController (the one that contains the UIScrollView in this case)
In the UIViewController that shows the modal view ... change this
[self presentModalViewController:controller animated:YES];
to this ...
[owningController presentModalViewController:controller animated:YES];
I'm not sure why you are using a UINavigationController. If it is because you would like your toWebMsgController controller to have a nav bar when it loads in the modal view, try the following alterations to your code:
if (self.toWebMsgController == nil) {
self.toWebMsgController = [[PostToWebMessageController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:toWebMsgController];
//[navController pushViewController:self.toWebMsgController animated:NO];
[self presentModalViewController:navController animated:YES];
If you don't require a nav bar in your modal view, you probably don't need a UINavigationController at all.
I had same issue.
Check that you root controller (if you present controller over it) for presentationStyle DOES NOT set to UIModalPresentationCurrentContext