I am in the process of upgrading an app from ios 7 to ios 8, and one of the areas I am having trouble with is the new UIPopoverPresentationController. For some reason, whenever I present a view controller using this class, the view controller does not appear in a popover, but instead presents like its being pushed onto the nav stack (takes up the whole screen). I'm sure I'm missing the obvious, but between Apple's documentation and the numerous Swift answers on SO I am missing it. Here's my code:
-(void)createAndSizePopover:(NSString*)tableName
{
//Create the picklist
self.pickListPopoverViewController = nil;
//NOTE WSPickListViewController is a UIViewController
self.pickListPopoverViewController = [[WSPickListViewController alloc] initWithNibName:nil bundle:nil withPickListItem:self.densityUnits andPickListTableName:tableName isSlimLine:YES];
self.pickListPopoverViewController.showSearchBar = NO;
self.pickListPopoverViewController.modalPresentationStyle = UIModalPresentationPopover;
((WSPickListViewController*)self.pickListPopoverViewController).pickListItemDelegate = self;
//Size the popover
NSInteger rowsCount = [self.pickListPopoverViewController.allObjects count];
NSInteger singleRowHeight = 35;
NSInteger totalRowsHeight = rowsCount * singleRowHeight;
NSInteger fourRowsHeight = 6 * singleRowHeight;
NSInteger height = (totalRowsHeight >= fourRowsHeight) ? fourRowsHeight : totalRowsHeight;
CGFloat largestLabelWidth = 0;
for (WSPickList* pickItem in self.pickListPopoverViewController.allObjects)
{
CGSize labelSize = [pickItem.name sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:20.0], NSForegroundColorAttributeName : [UIColor blackColor]}];
if (labelSize.width > largestLabelWidth)
{
largestLabelWidth = labelSize.width;
}
}
CGFloat popoverWidth = largestLabelWidth + 50;
[self.pickListPopoverViewController setPreferredContentSize:CGSizeMake(popoverWidth, height)];
}
-(void)showOrHidePopover:(id)sender withTableName:(NSString*)tableName
{
//Show/Hide the popover
if (self.popover != nil)
{
[self.pickListPopoverViewController dismissViewControllerAnimated:YES completion:nil];
self.popover = nil;
self.pickListPopoverViewController = nil;
return;
}
else
{
[self createAndSizePopover:tableName];
}
[self presentViewController:self.pickListPopoverViewController animated:YES completion: nil];
self.popover = self.pickListPopoverViewController.popoverPresentationController;
self.popover.permittedArrowDirections = UIPopoverArrowDirectionRight;
self.popover.sourceView = sender;
if ([sender isKindOfClass:[UIButton class]])
{
self.popover.sourceRect = ((UIButton*)sender).bounds;
}
else if ([sender isKindOfClass:[UICollectionViewCell class]])
{
self.popover.sourceRect = ((UICollectionViewCell*)sender).bounds;
}
}
I'm ok with an answer in either Objective-C or Swift (since I need to learn that anyway). Thanks in advance for any help provided!