弹出视图的坐标计算

时间:2013-03-14 09:56:12

标签: ios xcode ipad storyboard

在我的应用程序中,一个按钮被放置在视图控制器的底部。当点击该按钮时,我需要一个带有表格视图的弹出视图,它显示在我放置的按钮上方。我的问题是我的弹出视图的起始位置是在在我的视图控制器顶部。我需要在该按钮上方的一个小的弹出视图。我使用横向模式。

  UIViewController *popoverContent=[[UIViewController alloc]init];
UIView* popoverView = [[UIView alloc]initWithFrame:CGRectMake( 265,   100,  400 , 800)];
popoverView.backgroundColor = [UIColor clearColor];
// popoverView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mmf.png"]];


UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake( 0, 0,400,800) style:UITableViewStylePlain];
table.backgroundColor=[UIColor colorWithRed:(107.0/255.0) green: (164.0/255) blue:(183.0/255) alpha:1];
[table setDataSource:self];
[table setDelegate:self];
[table setRowHeight:80];
[popoverView addSubview:table];

popoverContent.view = popoverView;
// popoverContent.contentSizeForViewInPopover =
//CGSizeMake(250, 665);


self.popoverController = [[UIPopoverController alloc]
                          initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:popoverView.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[self.popoverController  presentPopoverFromRect:CGRectMake( 265,680,400,800)
                                         inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

我为坐标尝试了很多值,但它的起始位置不是我想要的位置。

4 个答案:

答案 0 :(得分:2)

修改

    UIViewController *popoverContent=[[UIViewController alloc]init];
    UIView* popoverView = [[UIView alloc]initWithFrame:CGRectMake( 0,   0,  400 , 400)];
    popoverView.backgroundColor = [UIColor clearColor];
    // popoverView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mmf.png"]];


    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake( 0, 0,400,400) style:UITableViewStylePlain];
    table.backgroundColor=[UIColor colorWithRed:(107.0/255.0) green: (164.0/255) blue:(183.0/255) alpha:1];
    [table setRowHeight:80];
    [popoverView addSubview:table];

    popoverContent.view = popoverView;
    popoverContent.contentSizeForViewInPopover = CGSizeMake(popoverView.frame.size.width, popoverView.frame.size.width);

    UIPopoverController *popoverController = [[UIPopoverController alloc]
                                              initWithContentViewController:popoverContent];
    [popoverController  presentPopoverFromRect:btnLogin.frame
                                        inView:btnLogin.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

注意 您只需确认frame of your button,并确保从view that contains button

出示

答案 1 :(得分:1)

试试这个,它会帮助你,

UIViewController* popoverContent = [[UIViewController alloc] init];
        UIView*   popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 260)];
        popoverView.backgroundColor = [UIColor whiteColor];

        UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake( 0, 0,320,260) style:UITableViewStylePlain];
    table.backgroundColor=[UIColor colorWithRed:(107.0/255.0) green: (164.0/255) blue:(183.0/255) alpha:1];
    [table setDataSource:self];
    [table setDelegate:self];
    [table setRowHeight:80];
    [popoverView addSubview:table];
        popoverContent.view = popoverView;
        popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
        popoverRect = CGRectMake(100, 200, 1,30);

      //    in the above line change the position of the popover controller 

        popoverController.popoverContentSize = CGSizeMake(320, 260);
        [popoverController 
         presentPopoverFromRect:popoverRect
         inView:self.view
         permittedArrowDirections:UIPopoverArrowDirectionUp
         animated:YES];

答案 2 :(得分:0)

提出popOver并不是一个好主意。用户从UI控件中呈现时更容易。

然而,尝试在UIView&的ViewDidLoad中设置contentPopOverSize。你的TableView。

[self setContentSizeForViewInPopover:CGSizeMake(width, height)];

您在CGRectMake中提供的框架应该与您的按钮相关。

希望它有所帮助。

答案 3 :(得分:0)

presentPopoverFromRect 是弹出窗口指向的矩形

[self.popoverController presentPopoverFromRect:yourBtn.frame inView:self.view    permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

请记住,youBt的框架是通过viewcontroller的视图计算的,如果你的按钮在任何其他视图中,那么它的坐标将被计算到该视图并且定位将不正确。