我正在将应用程序从ios 7升级到ios 8,而我遇到问题的一个方面是新的UIPopoverPresentationController。出于某种原因,每当我使用此类呈现视图控制器时,视图控制器不会出现在弹出框中,而是呈现为将其推入导航堆栈(占据整个屏幕)。我确定我错过了显而易见的内容,但在Apple的文档和众多Swift的答案之间,我错过了它。这是我的代码:
-(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;
}
}
我可以在Objective-C或Swift中找到答案(因为无论如何我都需要学习)。提前感谢您提供的任何帮助!
答案 0 :(得分:1)
经过几天的Google理论和测试理论,我终于在我的代码中发现了几个问题。
首先,在设置popoverPresentationController之前无法进行演示。我知道面对Apple的文档很苍白,甚至声称在设置popover控制器之前呈现视图控制器似乎“违反直觉”,但即使这仍然对我有用。
其次,我对adaptivePresentationStyleForPresentationController的实现:错了。我正在返回UIModalPresentationPopover,但我应该按照以下方式返回UIModalPresentationNone:
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
return UIModalPresentationNone;
}
希望这有助于其他人。