UIPopoverPresentationController width and height not setting - ios

I am trying to present a popover controller but for some reason I can't get the width/height of the popover to set correctly. Here's the code I am using:
Here's my code:
UIStoryboard* myStoryboard = self.storyboard;
ScenarioPopOver* popOver = (ScenarioPopOver*)[myStoryboard instantiateViewControllerWithIdentifier:#"scenarioPopover"];
popOver.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:popOver animated:YES completion:nil];
// configure the Popover presentation controller
UIPopoverPresentationController *popController = [popOver popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popController.delegate = self;
// in case we don't have a bar button as reference
popController.sourceView = self.view;
popController.sourceRect = CGRectMake(self.view.center.x - 25.0, 10.0, 50.0, 50.0);
But the popover keep coming out like this:

Just in case someone stumbles across this issue. I was trying to set the preferredContentSize on the UIPopoverPresentationController and not the UIViewController within it. Correct code is below:
UIStoryboard* myStoryboard = self.storyboard;
ScenarioPopOver* popOver = (ScenarioPopOver*)[myStoryboard instantiateViewControllerWithIdentifier:#"scenarioPopover"];
popOver.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:popOver animated:YES completion:nil];
// configure the Popover presentation controller
UIPopoverPresentationController *popController = [popOver popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popController.delegate = self;
popOver.preferredContentSize = CGSizeMake(400.0, 300.0);
// in case we don't have a bar button as reference
popController.sourceView = self.view;
popController.sourceRect = CGRectMake(self.view.center.x - 150.0, 10.0, 0.0, 0.0);

Related

ViewController content size doesn't change popover size

The popover's preferred size is only works if the height is greater than 320.
I have searched a lot to solve this issue. But didn't find any answer. Is there any minimum size that the popover should satisfy in iPad? If not, what i'm missing?
Code:
UIViewController *viewController = [subStoryboard instantiateViewControllerWithIdentifier:SMFormViewControllerSBID];
controller = [[UINavigationController alloc] initWithRootViewController:viewController];
SMFormViewController *formViewController = (SMFormViewController *)controller.topViewController;
formViewController.modalPresentationStyle = UIModalPresentationPopover;
formViewController.preferredContentSize = CGSizeMake(375, 320);
popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionLeft;
popController.delegate = self;
popController.sourceView = tblDocDetails;
NSInteger section = [tblDocDetails numberOfSections] - 1;
CGRect rectCustomLoc = [tblDocDetails rectForFooterInSection:section];
popController.sourceRect = CGRectMake(rectCustomLoc.origin.x, rectCustomLoc.origin.y, 130, rectCustomLoc.origin.y/2);
[self presentViewController:controller animated:YES completion:nil];
Note:
I tried, adaptivePresentationStyleForPresentationController: & adaptivePresentationStyleForPresentationController: to return UIModalPresentationNone.
I tried the following:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btnOpen = [UIButton buttonWithType:UIButtonTypeCustom];
[btnOpen setFrame:CGRectMake(100, 100, 100, 44)];
[btnOpen setTitle:#"POP" forState:UIControlStateNormal];
[btnOpen setBackgroundColor:[UIColor grayColor]];
[btnOpen addTarget:self action:#selector(btnOpen:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnOpen];
}
- (void)btnOpen:(id)sender {
UIView *sourceView = (UIButton *)sender;
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
vc.modalPresentationStyle = UIModalPresentationPopover;
vc.preferredContentSize = CGSizeMake(150, 150);
[self presentViewController:vc animated:YES completion:nil];
UIPopoverPresentationController *popVc = vc.popoverPresentationController;
popVc.permittedArrowDirections = UIPopoverArrowDirectionLeft;
popVc.sourceView = sourceView;
popVc.sourceRect = sourceView.bounds;
}
and it resulted in this (iPad Air 2, iOS 11.2):
Here is a solution based on your example, I think your use of GCD to show the popover might be causing the issue...
UIViewController *viewController = [subStoryboard instantiateViewControllerWithIdentifier:SMFormViewControllerSBID];
controller = [[UINavigationController alloc] initWithRootViewController:viewController];
controller.modalPresentationStyle = UIModalPresentationPopover;
controller.preferredContentSize = CGSizeMake(375, 320);
[self presentViewController:controller animated:YES completion:nil];
popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionLeft;
popController.delegate = self;
popController.sourceView = tblDocDetails;
NSInteger section = [tblDocDetails numberOfSections] - 1;
CGRect rectCustomLoc = [tblDocDetails rectForFooterInSection:section];
popController.sourceRect = CGRectMake(rectCustomLoc.origin.x, rectCustomLoc.origin.y, 130, rectCustomLoc.origin.y/2);
My popover sometimes doesn't visible, because of the sourceRect was incorrect.
Since I embedded ViewController in a Navigation Controller, preferredContentSize should set to Navigation Controller.
controller.preferredContentSize = CGSizeMake(375, 100);
& in ViewController:
self.navigationController.preferredContentSize = contentsize;
And Changed sourceRect to:
CGRect rectCustomLoc = [table rectForFooterInSection:section];
rectCustomLoc.size.width = sender.frame.size.width;
popController.sourceRect = rectCustomLoc;

Different navigation bar behaviour between CABTMIDILocalPeripheralViewController and CABTMIDICentralViewController

I am struggling to understand why the following views, one for CABTMIDILocalPeripheralViewController and the other for CABTMIDICentralViewController show different behaviour in my iOS app (written in Objective-C)
- (IBAction)configureCentral:(id)sender
{
CABTMIDICentralViewController *viewController = [CABTMIDICentralViewController new];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; // this will present a view controller as a popover in iPad and modal VC on iPhone
viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneAction:)];
navController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popC = navController.popoverPresentationController;
popC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popC.sourceRect = [sender frame];
viewController.preferredContentSize = CGSizeMake(320., 400.);
popC.backgroundColor = [UIColor darkGrayColor];
viewController.view.backgroundColor = [UIColor darkGrayColor];
UIButton *button = (UIButton *)sender;
popC.sourceView = button.superview;
[self presentViewController:navController animated:NO completion:nil];
}
Opens a popup view on an iOS device, which does not show any 'done' button to dismiss the view.
CABTMIDICentralViewController shows no button to close/dismiss the view
While on an iPad tapping outside the view will close it, on an iPhone the view takes the size of the screen and exiting/closing becomes impossible.
The same code used for the CABTMIDILocalPeripheralViewController works as expected.
- (IBAction)configureLocalPeripheral:(id)sender
{
CABTMIDILocalPeripheralViewController *vController = [[CABTMIDILocalPeripheralViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vController];
vController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneWithPeripheral:)];
navController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popC = navController.popoverPresentationController;
popC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popC.sourceRect = [sender frame];
vController.preferredContentSize = CGSizeMake(320., 400.);
popC.backgroundColor = [UIColor grayColor];
vController.view.backgroundColor = [UIColor darkGrayColor];
UIButton *button = (UIButton *)sender;
popC.sourceView = button.superview;
[self presentViewController:navController animated:NO completion:nil];
}
CABTMIDILocalPeripheralViewController has a 'done' button
What is going wrong here? I have tested to make the views wider to see whether available space in the Navigation bar would have caused this.
Thanks.

UIPopoverPresentationController can't setup arrow color

I am trying to present popover with UIPopoverPresentationController class on the iPhone.
I need to present navigation controller inside the popover. So I do the following:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
viewController.preferredContentSize = size;
navigationController.modalPresentationStyle = UIModalPresentationPopover;
self.currentPopoverController = navigationController.popoverPresentationController;
self.currentPopoverController.backgroundColor = [UIColor commonApplicationBgColor];//My common app blue color
self.view.alpha = 0.7;
[self presentViewController:navigationController animated:YES completion:nil];
It is working, but arrow colour is differ from the popover navigation bar background colour. I checked they use the same [UIColor commonApplicationBgColor], but it looks like arrow colour is darker. Tried setup alpha for self.currentPopoverController.backgroundColor, but it is still has wrong color.
Please check image:
if you don't want arrow then you can pass 0 to permittedarrowdirection like,
[popoverController presentPopoverFromRect:YOUR_RECT
inView:self.view
permittedArrowDirections:0
animated:YES];
Update :
you can set like,
popover = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
popover.backgroundColor = contentViewController.view.backgroundColor;
set bacground color to popovercontroller.
Update 2 :
myPopoverViewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:myPopoverViewController animated: YES completion: nil];
// Get the popover presentation controller and configure it.
UIPopoverPresentationController *presentationController =
[myPopoverViewController popoverPresentationController];
presentationController.permittedArrowDirections =
UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight;
presentationController.sourceView = myView;
presentationController.sourceRect = sourceRect;
presentationController.backgroundColor = [UIColor grayColor]; //set your expacted color here.
Hope this will help

iOS, PopOverPresentationController issue, .xib

I have a NavigationController, another controller was pushed on its stack: BNRDetailsViewController. Now, inside BNRDetailsViewController I am trying to show a popover window when pressing on a toolbar button, that would present me with UIImagePickerController (only on iPad devices). So, I tried to follow the following stackoverflow thread:
UIPopoverPresentationController on iOS 8 iPhone
But no success. If I just use their code I get an error that says, that pushing navigationController is not supported. If I try to push the newly created UIPopoverPresentationController like this: [self.navigationController pushViewController:self.imagePickerPopover animated:YES]; it crashes because UIPopoverPresentationController is not of type UIViewController, so, I guess, I can not just push it on the stack.
What would you suggest to do in this particular case?
Here is the code which I have right now that is triggered when the toolbar button is pressed:
- (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// If the device have camera, take a picture, otherwise,
// just pick from photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.delegate = self;
// Place image picker on the screen
//[self presentViewController:imagePicker animated:YES completion:NULL];
// Place image picker on the screen
// Check for iPad device before instantiating the popover controller
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// Create a new popover controller that will display the imagePicker
_imagePickerPopover = [[UIPopoverPresentationController alloc] initWithPresentedViewController:imagePicker presentingViewController:self];
imagePicker.preferredContentSize = CGSizeMake(280, 200);
_imagePickerPopover.delegate = self;
_imagePickerPopover.sourceView = self.view;
CGRect frame = [[sender valueForKey:#"view"] frame];
frame.origin.y = frame.origin.y + 20;
_imagePickerPopover.sourceRect = frame;
// [self.navigationController pushViewController:self.imagePickerPopover animated:YES];
}
}
Here is the code, based on your code, that will get the popover to display on the iPad.
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
imagePicker.modalPresentationStyle = UIModalPresentationPopover;
imagePicker.preferredContentSize = CGSizeMake(280, 200);
CGRect frame = [[sender valueForKey:#"view"] frame];
frame.origin.y = frame.origin.y + 20;
UIPopoverPresentationController *popoverController =
imagePicker.popoverPresentationController;
popoverController.sourceView = self.view;
popoverController.sourceRect = frame;
[self.navigationController presentViewController:imagePicker
animated:YES completion:nil];
}

UI gets distorted when dismissing modal view controller in iOS 8

modalViewController = [storyboard instantiateViewControllerWithIdentifier:#"transparentFullViewController"];
listViewController.view.frame = CGRectMake(0, 0, 900, 333);
CGPoint center=modalViewController.view.center;
listViewController.view.center=center;
[modalViewController addChildViewController:listViewController];
[modalViewController.view addSubview:listViewController.view];
listViewController.view.center = [UIApplication sharedApplication].keyWindow.rootViewController.view.center;
this is how i present the modal view controller
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:modalViewController animated:NO completion:nil];
and this is how it is dismissed
[modalViewController dismissViewControllerAnimated:NO completion:nil];
when I dismiss the modalViewController in iOS 7, it works fine and return to the 2nd screen shot
but when I dismiss it in iOS 8.0, then the view becomes distorted as shown in 1st screen shot
Any help is appreciated.
Thank You.
In iOS 8 try to use another presentation style:
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
you are doing wrong,
you wrote
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
but it should be like
modalViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

Resources