设置 UIPopOverController 大小

发布于 2024-11-25 09:07:38 字数 1157 浏览 8 评论 0原文

我有一个 view,其中有 UIScrollView 中的一堆按钮。当用户按下按钮时,我希望显示一个 UIPopOverController 指向所选按钮。它有点工作,但是弹出窗口的大小错误并且指向视图中的随机点。这是我的代码。

-(void)detail:(id)sender{
    UIButton *button = sender;
    NSLog(@"tag = %i", button.tag);

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    self.popover = [[UIPopoverController alloc] initWithContentViewController:navController];
    self.popover.delegate = self;
    [self.popover presentPopoverFromRect:button.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

比弹出窗口的大小问题: 在弹出窗口内的视图中,我有:

self.contentSizeForViewInPopover = scroll.contentSize;
NSLog(@"%f, %f", scroll.contentSize.height, scroll.contentSize.width);
NSLog(@"showing: %f, %f",  self.contentSizeForViewInPopover.height,  self.contentSizeForViewInPopover.width);

并且两个日志都匹配。所以我认为一切都应该正常进行。但事实并非如此。这是一个屏幕截图。如果您需要更多我的代码,请告诉我。提前致谢。

错误的屏幕截图

I have a view with a bunch of button in a UIScrollView. When the user presses a button, I want a UIPopOverController to display pointing at the selected button. It kind of works, but the popover is the wrong size and points to a random point in the view. Here is my code.

-(void)detail:(id)sender{
    UIButton *button = sender;
    NSLog(@"tag = %i", button.tag);

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    self.popover = [[UIPopoverController alloc] initWithContentViewController:navController];
    self.popover.delegate = self;
    [self.popover presentPopoverFromRect:button.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

Than the problem with the size of the popover:
In the view that is inside the popover, I have:

self.contentSizeForViewInPopover = scroll.contentSize;
NSLog(@"%f, %f", scroll.contentSize.height, scroll.contentSize.width);
NSLog(@"showing: %f, %f",  self.contentSizeForViewInPopover.height,  self.contentSizeForViewInPopover.width);

and both logs are matching. So I think everything should work correctly. But it doesn't. Here is a screen shot. Let me know if you need more of my code. Thanks in advance.

screen shot of the error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-12-02 09:07:38

我注意到的第一件事是,您应该对 from 矩形使用button.frame 而不是button.bounds。这些 UIView 属性之间的区别:

视图的几何形状由其框架、边界和中心定义
特性。框架属性定义了原点和尺寸
视图位于其父视图的坐标系中,通常是
在布局期间用于调整视图的大小或位置。

bounds 属性定义视图的内部尺寸
看到它们并且几乎只在自定义绘图代码中使用。

您可以使用其 popover 控制器设置高度<一href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPopoverController_class/Reference/Reference.html#//apple_ref/occ/instm/UIPopoverController/setPopoverContentSize%3aanimated%3a">setPopoverContentSize消息:

// so something like this ...
[self.popover setPopoverContentSize:CGSizeMake(320, 460)];
// and to bind the popover to your button 
[self.popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

First thing I noticed is that you should be using the button.frame not the button.bounds for the from rectangle. The difference between these UIView properties:

The geometry of a view is defined by its frame, bounds, and center
properties. The frame property defines the origin and dimensions of
the view in the coordinate system of its superview and is commonly
used during layout to adjust the size or position of the view.

The bounds property defines the internal dimensions of the view as it
sees them and is used almost exclusively in custom drawing code.

You can set the height of your popover controller using its setPopoverContentSize message:

// so something like this ...
[self.popover setPopoverContentSize:CGSizeMake(320, 460)];
// and to bind the popover to your button 
[self.popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
蓝色星空 2024-12-02 09:07:38

仅关于弹出窗口的大小:我设法使用可变高度的 UILabel 来做到这一点:

UILabel *hinweis;
hinweis.text = @"...";
hinweis.frame = CGRectMake(x,y,width,800);
[hinweis sizeToFit];

对于箭头:您是否尝试过不同的 inView 参数,例如 self.parentViewController.view?

Regarding the size of the popover only: I managed to do it with a variable heighted UILabel:

UILabel *hinweis;
hinweis.text = @"...";
hinweis.frame = CGRectMake(x,y,width,800);
[hinweis sizeToFit];

And for the arrow: have you tried a different inView param like self.parentViewController.view?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文