Ana*_*sia 5 iphone objective-c ipad uipopovercontroller ios
我UIPopoverController用UITableViewController它的内容视图控制器打开popover .
Popover具有合适的大小,乍一看它的内容视图调整正确,但如果我尝试在弹出窗口内滚动,它会滚动,但不应该.似乎在 - (void)viewDidAppear(图1,2)之后内容视图变得大约高20 px.我该怎么做来检查尺寸:
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"%@", self.view);
}
Run Code Online (Sandbox Code Playgroud)
输出是
Popovers[3016:c07] <UITableView: 0x10b80600; frame = (0 0; 200 66); autoresize = W+H; gestureRecognizers = <NSArray: 0x1004cc20>; layer = <CALayer: 0x1004c4a0>; contentOffset: {0, 0}>
Run Code Online (Sandbox Code Playgroud)
此时大小正确,它等于弹出窗口的大小.打开popover后,它的内部大小没有边距为66像素(在标尺应用程序的帮助下测量).因此,它似乎应该完全适合内容视图的大小.但意外的垂直滚动以某种方式出现.
比如果我将40 px添加到popover的高度,
[mOptionPopoverController setPopoverContentSize:(CGSize){200, 86}];
它的内容视图也会增加并且符合预期,没有任何滚动条.唯一的问题是我不需要在popover底部空20 px(图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
Run Code Online (Sandbox Code Playgroud)
pic.1

pic.2

pic.3

这就是我所期望的:

UPD.
我做了进一步的调查.我使用KVO来跟踪框架的变化.我期待它有助于找到有用的东西.我找到了一些东西,但它没有让事情更清楚.
所以,我[self.view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];在- (id)initWithStyle:(UITableViewStyle)styleTableViewController 的方法中添加了,观察方法是
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@", [object valueForKeyPath:keyPath]);
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
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}>
Run Code Online (Sandbox Code Playgroud)
(最后一个来自- (void)viewDidAppear:(BOOL)animated).
为什么84?它是84-66=18,而这些不是我正在寻找的那20个像素.此外,在调用tableView数据源方法之前,所有三个观察者方法的调用都会发生.在构建tableView之前如何知道框架?
(我目前使用的问题太多了@robmayoff tableView.scrollEnabled = NO;来抑制不必要的弹出窗口.)
UPD. 甚至Apple系统打印popover在打印机选择列表中也有20 px和不必要的滚动条.
这显然是 Apple Bug,因为在 WWDC 2013 期间曾向苹果工程师询问过这个问题,但没有得到答案。
解决方法是
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1f;
}
Run Code Online (Sandbox Code Playgroud)