无法在UIPopover的UINavigationController侧调整UIViewController的高度contentSizeForViewInPopover

时间:2013-07-30 09:51:09

标签: ios uinavigationcontroller uipopover

我在UINavigationController

中使用UIPopoverController
-(void)showEditMenuFrom:(UIButton *)button{
    if (self.popover) {
        [self.popover dismissPopoverAnimated:YES];
        self.popover = nil;
    }
    else {
        EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:@"EditMenuViewController" bundle:nil];

        UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
        actionsNavigationController.delegate = self;

        // switch for iPhone and iPad.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
            self.popover.delegate = self;
            //            CGRect presentFrame = CGRectMake(button.frame.origin.x-43, button.frame.origin.y-10, button.frame.size.width, button.frame.size.height);
            [self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        } else {
            [self presentViewController:actionsNavigationController animated:YES completion:^{
                NSLog(@"Activity complete");
            }];
        }
    }

    }

我尝试在导航时调整UIPopover内的视图大小,结果是:

  1. 根视图:
  2. enter image description here

    2.Push to other VC

    enter image description here

    3.Pop back:

    enter image description here

    您可以看到根VC的高度无法更改回原点大小。

    我尝试在contentSizeForViewInPopoverviewDidAppearviewDidLoad中设置UINavigationControllerDelegate,但没有方法可以使用

    -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        NSLog(@"Show VIEW %@",viewController);
        if ([viewController isKindOfClass:[EditMenuViewController class]]) {
            viewController.contentSizeForViewInPopover = CGSizeMake(160.0, 160.0);
        } else {
            viewController.contentSizeForViewInPopover = CGSizeMake(320.0, 320.0);
        }
    }
    

    如何解决这个问题?谢谢!

2 个答案:

答案 0 :(得分:2)

我最终得到满足此解决方案的人的问题:

首先,你需要在VC中声明一个@property,它在堆栈中弹回

@property (nonatomic, strong) UIPopoverController *popover;

然后在主代码

中将其分配给UIPopoverController
self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;

editMenuViewController.popover = self.popover;

最后,在VC的viewWillAppear中设置要调整大小的大小

-(void)viewWillAppear:(BOOL)animated
{
    [self.popover setPopoverContentSize:CGSizeMake(160,160)];
    self.contentSizeForViewInPopover = CGSizeMake(160,160);
    [super viewWillAppear:animated];
}

答案 1 :(得分:1)

尝试下面的例子miht是有用的: -

-(void)showEditMenuFrom:(UIButton *)button{
    if (self.popover) {
        [self.popover dismissPopoverAnimated:YES];
        self.popover = nil;
    }
    else {
        EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:@"EditMenuViewController" bundle:nil];

        UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
        actionsNavigationController.delegate = self;

        // switch for iPhone and iPad.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
            self.popover.delegate = self;

            popover.popoverContentSize =CGSizeMake(250,200); //Addint this line for setContentsize


            [self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        } else {
            [self presentViewController:actionsNavigationController animated:YES completion:^{
                NSLog(@"Activity complete");
            }];
        }
    }

    }

EditMenuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.clearsSelectionOnViewWillAppear = NO; //this work if superclass is UITableviewcontroller
     self.contentSizeForViewInPopover = CGSizeMake(200,100);
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.contentSizeForViewInPopover = CGSizeMake(250,200);
    [super viewWillDisappear:YES];
}

添加

检查一些有用的问题: -

Animate popoverContentsize when pushing navigation view controller in popover on iPad

UIPopoverController automatically resizing to max height on pushViewController

push a new tableViewController in a uipopovercontroller causes the popover to be resized

Popover with embedded navigation controller doesn't respect size on back nav