Keep popovercontentsize constant for all views in navigationcontroller - ios

When I pass my UINavigationController to UIPopOver and set its popOverContentSize property, it remains in effect for only first view. When I navigate to next view, the popOverContent size again expands to full view which is unwanted. How can I restrict the constant size of popOver for all views in navigation controller?
- (void)selectTextStyle:(id)sender {
DJTextStyleMasterViewController *textStyle = [[DJTextStyleMasterViewController alloc] init];
self.navController = [[UINavigationController alloc] initWithRootViewController:textStyle];
UIPopoverController *popOver = [[UIPopoverController alloc]
initWithContentViewController:self.navController];
[popOver setDelegate:self];
[popOver setPopoverContentSize:CGSizeMake(320, 400)];
[popOver presentPopoverFromBarButtonItem:self.textStyleButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[textStyle release];
}

within the first view controller in the popover controller you could use this method:
[self setContentSizeForViewInPopover:CGSizeMake(320, 400)];
Then the view of the popover (if it isn't manually modified) should be the same also for the next view controller.

Related

How to display a UIPopover from another class?

I have a iPad app (XCode 5, iOS 6, ARC and Storyboards), which has a class to display a UIView. From a separate class, I want to display a UIPopover under a certain condition.
I'm having a problem with the last line of code:
// create a popover for login or registration
UIViewController* popoverContent = [[UIViewController alloc]init];
UIView* popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 280, 180)];
popoverView.backgroundColor = [UIColor lightGrayColor];
popoverContent.view = popoverView;
//resize the popover view shown in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 280); // was 180
//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
// if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
}
// [popoverController presentPopoverFromRect:popoverView inView:self
// permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
SettingsViewController *svc = [[SettingsViewController alloc]init];
[popoverController presentPopoverFromRect: popoverView inView:svc.view.frame
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
It's telling me:
Sending 'UIView *__strong' to parameter of incompatible type 'CGRect' (aka 'struct CGRect')
How do I fix this? (I've tried different ways of presenting the popover, but none of them work).
Assuming the popoverView is the section of the svc view you want the popover to present from, your code should read:
[popoverController presentPopoverFromRect:popoverView.frame inView:svc.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
This will correct that error.
Edit: The reason it's probably not displaying for you is because you seem to be misunderstanding the presentPopoverFromRect method. The frame you indicate in the presentPopoverFromRect method needs to correspond the point of the view you'd like the popover to emerge from. For example, to have the popover present from the top left corner of the view, do something like this:
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:svc.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Can't change size for UIPopover?

I'm writing an app that needs to spawn a popover in order to add a new note. To that end, I have something that does kind of the trick, however, I can't seem to adjust the size of the popover. This is how I'm spawning it:
UIButton* btn =sender;
UIViewController* fooTroller = [[UIViewController alloc] init];
CGRect rectFoo = CGRectMake(0, 0, 100, 100);
UIView* fooView = [[UIView alloc] initWithFrame:rectFoo];
[fooView setBackgroundColor:[UIColor redColor]];
[fooTroller setView:fooView];
popOver =[[UIPopoverController alloc] initWithContentViewController:fooTroller];
[popOver presentPopoverFromRect:btn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
Any thoughts? It's not respecting the view size.
You should not be calling setView: on the view controller. Let the view controller setup its own view.
The proper way to size a popover is to either override the contentSizeForViewInPopover method of your view controller to return the size or to set the popoverContentSize property on the popover.
UIButton* btn =sender;
UIViewController* fooTroller = [[UIViewController alloc] init];
popOver = [[UIPopoverController alloc] initWithContentViewController:fooTroller];
popOver.popoverContentSize = CGSizeMake(100, 100);
[popOver presentPopoverFromRect:btn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];

iPad center custom modal view

I have an application with custom modal view controller and this controller is inside navigation controller here is my code:
CalendarViewController *calendarViewController = [[CalendarViewController alloc] initWithNibName:#"CalendarViewController" bundle:nil];
[calendarViewController setDelegate:self];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:calendarViewController];
[navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
[navigationController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:navigationController animated:YES];
navigationController.view.superview.frame = CGRectMake(0, 0, 630, 555);
navigationController.view.superview.center = self.view.center;
Problem is that this navigation controller is not center on screen. Location of this screen is right side of ipad. Is there a solution how center this custom controller. Thanks a lot.

How to display a popover programmatically from a uibutton that is also created programmatically (Not using interface builder)

I have a button I have created programmatically within a view controller. Once the button is pressed I want it to to use a method to create the popover programmatically.
The button which is created in the ViewDidLoad in my view controller.m
UIView *moreFundInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
[self.view addSubview:moreFundInfoView];
[moreFundInfoView setBackgroundColor:[UIColor RMBColor:#"b"]];
btnContact = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[btnContact setFrame:CGRectMake(390, 575, contactButton.width, contactButton.height)];
btnContact.hidden = NO;
[btnContact setTitle:#"Contact" forState:(UIControlStateNormal)];
[moreFundInfoView addSubview:btnContact];
[btnContact addTarget:self action:#selector(showContactDetails:) forControlEvents:UIControlEventTouchUpInside];
Then I have the method I use when the button is pressed.
-(void) showContactDetails: (id) sender
{
UIViewController *popoverContent = [[UIViewController alloc]init];
UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
[popoverView setBackgroundColor:[UIColor RMBColor:#"b"]];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);
UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:popoverContent];
[contactPopover presentPopoverFromRect:btnContact.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];
[contactPopover setDelegate:self];
}
What am I missing here? Cause it runs fine, but as soon as I click the button the app crashes. I think it is a delegate issue, but I am not sure. Any advice would be appreciated.
I think this code will help you. You are certainly missing delegate methods
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(644, 425); //your custom size.
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Just make sure you are not forgetting UIPopover Delegate methods or else application will definitely crash. It is mandatory.
UIViewController *controller = [[UIViewController alloc] init];
[view removeFromSuperview]; //view is a view which is displayed in a popover
controller.view = view;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
popover.delegate = self;
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
All I had to do was change the property from "retain" to "strong" in the .h file and it works, stopped the app from crashing.
Yes, changing property for "retain" to "strong" makes you to hold your picker view object.
I think the problem with your code was, UIPopoverController object gets deallocated automatically when method finishes.
making strong property gets you to strongly point an object.

How to dismiss a popover with a navigation controller?

I have an app which is divided in four views, when I tap a view the app shows a popover and that popover has a navigation controller. I can navigate between view and the data is shows correctly. But when I am in the last view and I tap a row, the popover don't dismiss (I want in didSelectRowAtIndexPath).
How could do it?
I tried in this way, I created a method that dismiss the popover and I call this method en didSelectRowAtIndexPath, but did not works.
This is the method in the main ViewController
-(IBAction)mostrarTabla:(id)sender
{
// Popover that shows the table
UIPopoverController *popover;
// RootViewController is the first view
RootViewController *rootViewController = [[RootViewController alloc] init];
// With a nav bar
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[rootViewController release];
// Popover customitation
navBar.contentSizeForViewInPopover = CGSizeMake(20.0f, 20.0f);
popover = [[UIPopoverController alloc] initWithContentViewController:navBar];
[navBar release];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(320.0f, 832.0f);
// PopOver is shows in the view 1
[popover presentPopoverFromRect:CGRectMake(100.0f, 10.0f, 20.0f, 20.0f) inView:_view1 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I create a method in this View Controller
-(void)hidePopover
{
[self.popOver dismissPopoverAnimated:YES];
}
And in the last view I used the method but did not works
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *grafica = [[ViewController alloc] init];
self.indicadorId = [[self.arrayId objectAtIndex:indexPath.row] integerValue];
DataIndicador *datos =[[DataIndicador alloc] init];
datos.idIndicador = self.indicadorId;
[datos release];
[grafica hidePopover];
}
In the last view I want that the popover returns to its view and shows the data (a chart)
Thanks
The grafica instant of the ViewController is not the same as the whatever instant of VieController that was originally presented the popover.

Resources