Weakman10122 Weakman10122 - 1 month ago 5
iOS Question

iOS: Autolayout and popOverController issue (possible Xcode bug)

I am displaying my

for iPhone and iPad in the code below.

ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];

if ([UIDevice currentDevice].userInterfaceIdiom ==
UIUserInterfaceIdiomPad) {
vc.preferredContentSize = CGSizeMake(296, 476); //your custom size.
} else {
vc.preferredContentSize = CGSizeMake(252, 436.5);

vc.modalPresentationStyle = UIModalPresentationPopover;
vc.popoverPresentationController.delegate = self;
vc.popoverPresentationController.sourceView = self.view;
vc.popoverPresentationController.sourceRect = CGRectMake(screenWidth / 2, self.view.height / 2, 1, 1);

[self presentViewController:vc animated:YES completion:nil];

UIPopoverPresentationController *popOverController = vc.popoverPresentationController;
popOverController.permittedArrowDirections = 0;

However, there seems to be a class size issue occurring. It seems that iPhone size is being used and not the iPad size at all even when running an iPad device. Eventhough in interface builder it shows two different sizes for the buttons depending on the device that is selected.

Edit: For example, I have a viewcontroller.xib that has buttons. There two different button sizes depending on the actual device. On the iPhone, the buttons have a dimension of 50 x 50. On iPad it has a dimension of 430 x 430. In interface builder, this shows up correctly. However, when running an iPad device, the buttons show as 50 x 50 when it suppose to be 430 x 430.


The conditional constraints in the storyboard use size classes.

The horizontal size class on a popover-presented view controller on an iPad is Compact, just like on an iPhone. That is why you are seeing the iPhone size for the buttons.

To see that this is true, override viewDidAppear: in your ViewController class to NSLog self.traitCollection. You will see that the horizontal size class is Compact.

This makes sense, because a popover is like an iPhone-sized window appearing on the iPad. That, in fact, is the whole point of a popover.