简体   繁体   English

弹出窗口内容视图的额外高度

[英]Extra height of popover's content view

I open UIPopoverController popover with UITableViewController as it's content view controller. 我用UITableViewController作为内容视图控制器打开UIPopoverController

Popover has the proper size, and it's content view at first glance adjusts correct, but if I try to scroll inside popover, it scrolls, though it shouldn't. Popover具有合适的大小,乍一看它的内容视图调整正确,但如果我尝试在弹出窗口内滚动,它会滚动,但不应该。 Seems that the content view becomes about 20 px higher after -(void)viewDidAppear (pic 1, 2). 似乎在 - (void)viewDidAppear(图1,2)之后内容视图变得大约高20 px。 What do I do to check sizes: 我该怎么做来检查尺寸:

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%@", self.view);
}

Output is 输出是

Popovers[3016:c07] <UITableView: 0x10b80600; frame = (0 0; 200 66); autoresize = W+H; gestureRecognizers = <NSArray: 0x1004cc20>; layer = <CALayer: 0x1004c4a0>; contentOffset: {0, 0}>

At this point the size is correct, and it is equal to the size of popover. 此时大小正确,它等于弹出窗口的大小。 After popover is opened it's inner size without margins is 66 px (measured with the help of Ruler app). 打开popover后,它的内部大小没有边距为66像素(在标尺应用程序的帮助下测量)。 So, it seems that it should fit exactly to the size of content view. 因此,它似乎应该完全适合内容视图的大小。 But unexpected vertical scrolling appears somehow. 但意外的垂直滚动以某种方式出现。

Than, if I add 20 px to the height of popover, [mOptionPopoverController setPopoverContentSize:(CGSize){200, 86}]; [mOptionPopoverController setPopoverContentSize:(CGSize){200, 86}]; ,如果我将20 px添加到[mOptionPopoverController setPopoverContentSize:(CGSize){200, 86}];的高度, [mOptionPopoverController setPopoverContentSize:(CGSize){200, 86}]; than it's content view also increases and fits as expected, without any scrollers. 而且它的内容视图也增加并且符合预期,没有任何滚动条。 Only issue is that I don't need empty 20 px at the bottom of popover (pic. 3). 唯一的问题是我不需要在popover底部空20 px(图3)。

I made a small test project to reproduce my problem. 我做了一个小测试项目来重现我的问题。 Here is the important code: 这是重要的代码:

@implementation ViewController

- (IBAction)buttonPressed:(id)sender
{
    TableViewController *popoverViewController = [[TableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UIPopoverController* mOptionPopoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
    [mOptionPopoverController setPopoverContentSize:(CGSize){200, 66}]; 
    CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];
    [mOptionPopoverController presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.tableView.bounces = NO;
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    return cell;
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%@", self.view);
}

@end

pic.1 pic.1 在此输入图像描述

pic.2 pic.2 在此输入图像描述

pic.3 pic.3 在此输入图像描述

And here is what I would expect: 这就是我所期望的: 在此输入图像描述

UPD. UPD。

I've made further investigation. 我做了进一步的调查。 I used KVO to follow the changes of frame. 我使用KVO来跟踪框架的变化。 I was expecting it will help to find something useful. 我期待它有助于找到有用的东西。 And I found something, but it didn't make things clearer. 我找到了一些东西,但它没有让事情更清楚。

So, I added [self.view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]; 所以,我添加了[self.view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]; in the - (id)initWithStyle:(UITableViewStyle)style method of TableViewController, and the observing method is 在TableViewController的- (id)initWithStyle:(UITableViewStyle)style方法中,观察方法是

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"%@", [object valueForKeyPath:keyPath]);
}

And here is output: 这是输出:

2013-05-27 15:52:12.755 Popovers[1547:c07] NSRect: {{0, 0}, {200, 84}}
2013-05-27 15:52:12.756 Popovers[1547:c07] NSRect: {{0, 0}, {200, 84}}
2013-05-27 15:52:12.756 Popovers[1547:c07] NSRect: {{0, 0}, {200, 66}}
2013-05-27 15:52:12.759 Popovers[1547:c07] <UITableView: 0x9337600; frame = (0 0; 200 66); autoresize = W+H; gestureRecognizers = <NSArray: 0x8949760>; layer = <CALayer: 0x8944690>; contentOffset: {0, 0}>

(the last one is from - (void)viewDidAppear:(BOOL)animated ). (最后一个来自- (void)viewDidAppear:(BOOL)animated )。

Why 84? 为什么84? It's 84-66=18 , and these are not those 20 px that I'm looking for. 这是84-66=18 ,这些不是我正在寻找的20 px。 Additionally, all of three calls of observer's method are happen before tableView data source method are called. 此外,在调用tableView数据源方法之前,所有三个观察者方法的调用都会发生。 How the frame can be known before tableView is built? 在构建tableView之前如何知道框架?

(Having too many questions I currently use suggested by @robmayoff tableView.scrollEnabled = NO; to suppress unnecessary popovers.) (我目前使用的问题太多了@robmayoff tableView.scrollEnabled = NO;来抑制不必要的弹出窗口。)

UPD. UPD。 Even Apple system printing popover has that 20 px and unnecessary scroller in the printer selection list. 甚至Apple系统打印popover在打印机选择列表中也有20 px和不必要的滚动条。

This is apparently the Apple Bug, as this question has been asked apple engineers during WWDC 2013 and got no answer. 这显然是Apple Bug,因为这个问题在WWDC 2013期间被苹果工程师提出并且没有得到答案。

The workaround is 解决方法是

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

尝试设置表视图的滚动启用属性,如下所示。

tableView.scrollEnabled = NO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM