私はこのコードをインターネットのどこかで見つけました。ただし、これは完全に機能します (これは UITableViewController サブクラスにあります)。コントローラーの幅を、表に表示されている最も広い線よりも少し広く設定します (私の場合、開示インジケーターの余地があります)。
self = [super initWithStyle:style];
if (self)
{
sortOrders = @[@"Alphabetic",@"User-assigned",@"Randomized"];
selectedSortOrder = @"Alphabetic";
//
// compute the size of the popover based on maximum width and required height to accommodate all rows
//
int rowHeight = [self.tableView.delegate tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
int totalRowHeight = [sortOrders count] * rowHeight;
CGFloat maxWidth = 0.0f;
for (NSString *s in sortOrders)
{
CGSize labelSize = [s sizeWithFont:[UIFont systemFontOfSize:14.0f]];
if (labelSize.width > maxWidth)
maxWidth = labelSize.width;
}
self.contentSizeForViewInPopover = CGSizeMake(maxWidth+50.0f, totalRowHeight);
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
return self;
}