iphone - 弹出窗口内容 View 的额外高度

标签 iphone ios objective-c ipad uipopovercontroller

我用 UITableViewController 打开 UIPopoverController 弹出窗口,因为它是内容 View Controller 。

Popover 具有适当的大小,乍一看它的内容 View 调整正确,但如果我尝试在 popover 内滚动,它会滚动,但它不应该滚动。似乎内容 View 在 -(void)viewDidAppear(图 1、2)之后变得高出约 20 像素。 我该怎么做才能检查尺寸:

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

输出是

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

此时大小正确,等于popover的大小。打开弹出窗口后,它的内部尺寸(没有边距)为 66 px(借助 Ruler 应用程序测量)。因此,它似乎应该完全适合内容 View 的大小。但是不知何故出现了意想不到的垂直滚动。

然后,如果我将弹出框的高度增加 20 像素, [mOptionPopoverController setPopoverContentSize:(CGSize){200, 86}]; 比它的内容 View 也增加并按预期适合,没有任何滚动条。唯一的问题是我不需要在弹出窗口底部留出 20 像素的空白区域(图 3)。

我做了一个小测试项目来重现我的问题。这是重要的代码:

@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

图1 enter image description here

图2 enter image description here

图3 enter image description here

这就是我所期望的: enter image description here

UPD。

我做了进一步的调查。 我使用 KVO 来跟踪框架的变化。我期待它会帮助找到有用的东西。我发现了一些东西,但并没有让事情变得更清楚。

因此,我在 - (id)initWithStyle:(UITableViewStyle) 样式中添加了 [self.view addObserver:self forKeyPath:@"frame"options:NSKeyValueObservingOptionNew context:nil]; TableViewController的方法,观察方法为

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

这是输出:

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}>

(最后一个来自 - (void)viewDidAppear:(BOOL)animated)。

为什么是 84?它是 84-66=18,这些不是我要找的那些 20 像素。 此外,观察者方法的三个调用都发生在调用 tableView 数据源方法之前。如何在 tableView 构建之前知道框架?

(我目前使用@robmayoff tableView.scrollEnabled = NO 建议的问题太多,以抑制不必要的弹出窗口。)

更新。 甚至 Apple 系统打印弹出窗口在打印机选择列表中也有 20 像素和不必要的滚动条。

最佳答案

这显然是 Apple Bug,因为这个问题已在 WWDC 2013 期间问过苹果工程师,但没有得到答案。

解决方法是

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

关于iphone - 弹出窗口内容 View 的额外高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740837/

相关文章:

iphone - 链接到框架是什么意思? (iOS)

iphone - 什么时候触发自动释放池

ios - 异步加载图像,单元格中的图像错误

ios - UIWebview中的嵌套stringByEvaluatingJavaScript迁移到WKWebview

ios - NSRegularExpression分隔段落

iphone - iOS 在 iOS 6 + 7 中隐藏状态栏和标签栏

iphone - 带有 alpha 掩码的 AVPlayerLayer

javascript - 在 iOS Safari 上借助 getUsermedia 访问麦克风

ios - 在 iOS 11 中切换任务时出现虚假旋转事件

ios - 如何检测我是否拥有 iPhone 2G,3G,3GS