ios 在哪里设置弹出框大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11836294/
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 19:37:06  来源:igfitidea点击:

Where to set popover size?

objective-ciosuitableview

提问by Nosrettap

I have a UIViewControllercalled HomeViewControllerand it has a model that contains an array of data. HomeViewControlleralso has a button that when pressed will show a UITableViewControllerthat display's the model's array of data.

我有一个UIViewController电话HomeViewController,它有一个包含数据数组的模型。 HomeViewController还有一个按钮,按下该按钮时将显示UITableViewController该显示器的模型数据数组。

My question is, what is the standard/best way to set the popover's size? I only want the popover to be tall enough to display the data, but no taller. What is common practice? I assume that I need to set contentSizeForViewInPopoverat some point but I'm not sure where...In the viewDidLoadmethod of the popover class? In the prepareForSeguemethod?

我的问题是,设置弹出框大小的标准/最佳方法是什么?我只希望弹出框足够高以显示数据,但不能更高。什么是普遍做法?我假设我需要contentSizeForViewInPopover在某个时候设置,但我不确定在哪里...在viewDidLoadpopover 类的方法中?在prepareForSegue方法上?

Here's the code I have so far: (Note that DataPresentingViewControlleris my popover view controller class`)

这是我到目前为止的代码:(请注意,这DataPresentingViewController是我的弹出视图控制器类)

//In HomeViewController
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.destinationViewController isKindOfClass:[DataPresentingViewController class]])
    {
        DataPresentingViewController *dest = (DataPresentingViewController *) segue.destinationViewController;
        dest.mySavedData = self.myModel.mySavedData;
    }
}

I know that I could just set the contentSizeForViewInPopoverhere in the prepareForSeguemethod; however, this seems like something the popover class should deal with.

我知道我可以contentSizeForViewInPopoverprepareForSegue方法中设置here ;然而,这似乎是 popover 类应该处理的事情。

回答by LuisCien

As of iOS7 contentSizeForViewInPopoveris deprecated. You should use UIViewController's preferredContentSizeproperty instead.

自 iOS7 起contentSizeForViewInPopover已弃用。您应该改用UIViewControllerpreferredContentSize属性。

Hope this helps!

希望这可以帮助!

回答by Azat

set contentSizeForViewInPopoverin ViewDidLoadmethod in HomeViewController

contentSizeForViewInPopoverViewDidLoad方法中设置HomeViewController

回答by Vacca

contentSizeForViewInPopoveris deprecated in iOS 7
The property: popoverContentSizeof UIPopoverController represents the size of the content view that is managed by the view controller in the contentViewController property of UIPopoverController.
Reference

contentSizeForViewInPopover已被弃用在IOS 7
的属性:popoverContentSize的UIPopoverController表示由在UIPopoverController的contentViewController属性视图控制器管理的内容视图的大小。
参考

The advantage is that it is available in iOS 3.2 and later, so you don't need to check device version everytime, just replace contentSizeForViewInPopovermethod with your UIPopOverController object instance.

优点是它在 iOS 3.2 及更高版本中可用,因此您无需每次都检查设备版本,只需将contentSizeForViewInPopover方法替换为您的 UIPopOverController 对象实例。

回答by CocoaEv

Have you tried:

你有没有尝试过:

setPopoverContentSize:<#(CGSize)#> animated:<#(BOOL)#>

in your segue logic block. Useful if you want the popover size to be variable based upon a data set, or view placement or whatever.

在您的 segue 逻辑块中。如果您希望弹出框大小根据数据集或视图位置或其他内容可变,则很有用。

回答by Beyond 2021

// self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); //Deprecated in ios7


self.preferredContentSize = CGSizeMake(320.0, 600.0); //used instead

回答by Douglas

It looks like you want to do it programmatically, but in the storyboard, before you hook up a view controller to a segue, under the attributes inspector there is a "popover, use explicit size option." Maybe you can set the size that would work the best for your App first and not worry about the size with using code. Hope this helps, let us know how it goes.

看起来您想以编程方式执行此操作,但是在情节提要中,在将视图控制器连接到 segue 之前,在属性检查器下有一个“弹出窗口,使用显式大小选项”。也许您可以先设置最适合您的应用程序的大小,而不必担心使用代码的大小。希望这会有所帮助,让我们知道它是怎么回事。

回答by Nvork

Try the following code:

试试下面的代码:

- (IBAction)setData:(id)sender
{
    UIViewController *popoverContent=[[UIViewController alloc] init];
    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(265, 680, 0, 0) style:UITableViewStylePlain];

     UIView *popoverView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];

     popoverView.backgroundColor=[UIColor whiteColor];

    popoverContent.view=popoverView;
    popoverContent.contentSizeForViewInPopover=CGSizeMake(200, 420);//setting the size
    popoverContent.view=tableView;
    tableView.delegate=self;
    tableView.dataSource=self;

    self.popoverController=[[UIPopoverController alloc] initWithContentViewController:popoverContent];
    [self.popoverController presentPopoverFromRect:CGRectMake(400, 675, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}

回答by memmons

A little known trick to sizing your UIPopoverViewControllerto match the height of your UITableViewis to use the tableView's rectForSectionmethod to give you the height. Use the height in your viewController's contentSizeForViewInPopoverlike this:

调整您UIPopoverViewController的高度以匹配您的高度的一个鲜为人知的技巧UITableView是使用 tableView 的rectForSection方法为您提供高度。contentSizeForViewInPopover像这样使用 viewController 中的高度:

- (CGSize)contentSizeForViewInPopover {
    // Currently no way to obtain the width dynamically before viewWillAppear.
    CGFloat width = 200.0; 
    CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
    CGFloat height = CGRectGetMaxY(rect);
    return (CGSize){width, height};
}