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
Where to set popover size?
提问by Nosrettap
I have a UIViewController
called HomeViewController
and it has a model that contains an array of data. HomeViewController
also has a button that when pressed will show a UITableViewController
that 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 contentSizeForViewInPopover
at some point but I'm not sure where...In the viewDidLoad
method of the popover class? In the prepareForSegue
method?
我的问题是,设置弹出框大小的标准/最佳方法是什么?我只希望弹出框足够高以显示数据,但不能更高。什么是普遍做法?我假设我需要contentSizeForViewInPopover
在某个时候设置,但我不确定在哪里...在viewDidLoad
popover 类的方法中?在prepareForSegue
方法上?
Here's the code I have so far: (Note that DataPresentingViewController
is 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 contentSizeForViewInPopover
here in the prepareForSegue
method; however, this seems like something the popover class should deal with.
我知道我可以contentSizeForViewInPopover
在prepareForSegue
方法中设置here ;然而,这似乎是 popover 类应该处理的事情。
回答by LuisCien
As of iOS7 contentSizeForViewInPopover
is deprecated. You should use UIViewController
's preferredContentSize
property instead.
自 iOS7 起contentSizeForViewInPopover
已弃用。您应该改用UIViewController
的preferredContentSize
属性。
Hope this helps!
希望这可以帮助!
回答by Azat
set contentSizeForViewInPopover
in ViewDidLoad
method in HomeViewController
contentSizeForViewInPopover
在ViewDidLoad
方法中设置HomeViewController
回答by Vacca
contentSizeForViewInPopover
is deprecated in iOS 7
The property: popoverContentSize
of 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 contentSizeForViewInPopover
method 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 UIPopoverViewController
to match the height of your UITableView
is to use the tableView's rectForSection
method to give you the height. Use the height in your viewController's contentSizeForViewInPopover
like 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};
}