ios 如何动态改变 UIPopoverController 的 contentSize?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8516736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:52:57  来源:igfitidea点击:

How To Dynamically change the contentSize of UIPopoverController?

objective-cioscocoa-touchipaduipopovercontroller

提问by Avi Shukron

I have a UIViewControllerthat contains a UITableView. This UIViewControlleris being displayed in a UIPopoverController.

我有一个UIViewController包含UITableView. 这UIViewController显示在UIPopoverController.

Now, the things is that the number of items in the tableViewis not constant, and I want the size of the popover (that is - the popoverContentSize), to adjust according to the number of items in the tableView

现在,事情是项目的数量tableView不是恒定的,我想要弹出框的大小(即 - popoverContentSize),根据项目的数量进行调整tableView

Naively, I was thinking that if I'll set the contentSizeForViewInPopoverin viewDidLoadafter I'm loading the tableViewwith all the items - It'll do it.

天真,我在想,如果我将设置contentSizeForViewInPopoverviewDidLoad我加载后,tableView所有的项目-它会做到这一点。

It didn't.

它没有。

So the make it short, my question is: How can I change the popoverContentSizedirectly from the contentViewController- after it's been presented?

所以简而言之,我的问题是:我如何popoverContentSize直接从contentViewController- 呈现后更改?

Appendix: enter image description here

附录: 在此处输入图片说明

回答by Avinash

I might be very late to answer but for new user from iOS 7 please use the following line in your UIViewControlleri,e contentViewControllerof your UIPopOverViewConotroller

我可能会很晚才回答,但对于来自 iOS 7 的新用户,请在您的UIViewControlleri,e contentViewControllerof UIPopOverViewConotroller 中使用以下行

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.preferredContentSize=myTableView.contentSize;
}

Hope this will help for iOS 7 user.

希望这对 iOS 7 用户有所帮助。

回答by Avi Shukron

Well, In the end i did something that I'm not sure if it's the right thing to do, but it is working.

好吧,最后我做了一些我不确定它是否正确的事情,但它正在起作用。

I added a reference in my contentViewController to the popoverController:

我在 contentViewController 中添加了一个对 popoverController 的引用:

@property (nonatomic , assign) UIPopoverController *popoverControllerContainer;

Then, I added the resizing code to viewWillAppear and viewDidAppear:

然后,我将调整大小代码添加到 viewWillAppear 和 viewDidAppear:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView reloadData];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.contentSizeForViewInPopover = self.tableView.contentSize;
}

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.popoverControllerContainer setPopoverContentSize:self.contentSizeForViewInPopover animated:YES];
}

So, keeping a reference to the popover is kind of hack-ish, so I'm open to hear better ideas.

所以,保留对 popover 的引用有点像黑客,所以我很乐意听到更好的想法。

回答by Padin215

A UIViewControllerclass has the property

一个UIViewController类具有属性

self.contentSizeForViewInPopover

which will resize the pop over w/out needing to adding a reference to it.

这将调整弹出窗口的大小,而无需添加对它的引用。

And to expand on a solution, i used the method rectForSection:to get the size of the section (mine only has 1 section, so easy enough to get) and then added the height of the navigation bar (it seems to be 20). so i'm able to create the popover the size of the finished table view:

为了扩展解决方案,我使用了rectForSection:方法来获取部分的大小(我的只有 1 个部分,很容易获得),然后添加导航栏的高度(似乎是 20)。所以我可以创建完成表视图大小的弹出窗口:

CGRect sectionRect = [view.tableView rectForSection:0];

if (sectionRect.size.height + 20 < POPOVER_SIZE.height)
    view.contentSizeForViewInPopover = CGSizeMake(POPOVER_SIZE.width, sectionRect.size.height + 20);
else
    view.contentSizeForViewInPopover = POPOVER_SIZE;

might prove more difficult with multiple sections, i didn't try it. should just be able to sum up the section heights, but there might be some spacing issues that i don't know about.

多部分可能会更难,我没有尝试。应该能够总结出截面高度,但可能存在一些我不知道的间距问题。

回答by Robert

For iOS 7 or above.

适用于 iOS 7 或更高版本。

- (CGSize)preferredContentSize {
    return CGSizeMake(320, 550);
}

If you are a child of a container, re-direct the content size to yourself. E.g. In a UINavigationControllersubclass:

如果您是容器的子项,请将内容大小重定向到您自己。例如在UINavigationController子类中:

- (CGSize)preferredContentSize {
    return self.topViewController.preferredContentSize;
}

回答by Mojo66

My app has a menu with sub-menus originating from a popover and embedded in a navigation controller, i.e. something like UINavigationController-> TopMenuViewController-> SubMenuViewControllerxN.

我的应用程序有一个菜单,其子菜单源自弹出框并嵌入到导航控制器中,例如UINavigationController-> TopMenuViewController-> SubMenuViewControllerxN。

None of the above solutions worked for me.

以上解决方案都不适合我。

My solution: In my root menu view controller class, from which all other menu view controllers inherit, add the following code:

我的解决方案:在我所有其他菜单视图控制器继承的根菜单视图控制器类中,添加以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.preferredContentSize  = CGSizeMake(450.0f, 0.0f);// increase standard width (320.0)
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.preferredContentSize = self.tableView.contentSize;
}

回答by user431791

This should do the trick

这应该可以解决问题

override func viewWillLayoutSubviews() {
  super.viewWillLayoutSubviews()

  self.preferredContentSize = CGSizeMake(0, self.tableView.contentSize.height)}

回答by capikaw

iOS8/9 solution - just override preferredContentSizeand force the table view to layout prior to returning it's contentSize.

iOS8/9 解决方案 -preferredContentSize在返回它的contentSize.

- (CGSize)preferredContentSize {
    [self.tableView layoutIfNeeded];
    return self.tableView.contentSize;
}

回答by Bryan Schmiedeler

I was having this same problem and none of the answers I found worked. I cobbled together that I think works well.

我遇到了同样的问题,我找到的答案都没有。我拼凑起来,我认为效果很好。

I have an array in my initWithCoder that holds my values. I simply run this code:

我的 initWithCoder 中有一个数组,用于保存我的值。我只是运行这个代码:

- (id)initWithCoder:(NSCoder *)aDecoder
{
//Popover Choices

_importantChoices = [NSMutableArray array];
[_importantChoices addObject:@"Bakery"];
[_importantChoices addObject:@"Beverage Dry Mix"];
[_importantChoices addObject:@"Beverage RTD"];
[_importantChoices addObject:@"Crisps"];
[_importantChoices addObject:@"Meat"];
[_importantChoices addObject:@"Supplements"];


//Calculate size of Popover
self.clearsSelectionOnViewWillAppear = NO;
NSInteger rowsCount = [_importantChoices count];
NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView
                                       heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSInteger totalRowsHeight = rowsCount * singleRowHeight;
CGFloat largestLabelWidth = 0;
for (NSString *tmp in _importantChoices) {
    CGSize labelSize = [tmp sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0f]}];
    if (labelSize.width > largestLabelWidth) {
        largestLabelWidth = labelSize.width;
    }
}
CGFloat popoverWidth = largestLabelWidth + 100;     //Add a little padding to the width
self.preferredContentSize = CGSizeMake(popoverWidth, totalRowsHeight);
return self;
}