Animate popoverContentsize when pushing navigation view controller in popover on iPad

时光总嘲笑我的痴心妄想 提交于 2019-12-03 05:22:57

Try to do the following for all UITableViewController, I tried it and it works for me!

- (void)setSize:(BOOL)fake
{
    CGSize view_size;
    int sections_height, rows_height;

    //if you dynamically change the number of visible rows
    //for example overriding numberOfRowsInSection:section
    //this can help you to evaluate the correct popover height
    int sections = [self.tableView numberOfSections];
    int rows = 0;
    for(int i = 0; i < sections; i++){
        rows += [self.tableView numberOfRowsInSection:i];
    }
    sections_height = sections * 30 /*section height*/;
    rows_height = rows * 44 /*row height*/;

    view_size = CGSizeMake(320 /*fixed width*/, sections_height + rows_height);

    if(fake){
        view_size.width -= 1;
        view_size.height -= 1;
    }

    [self setContentSizeForViewInPopover:view_size];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //...    
    [self setSize:FALSE];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setSize:TRUE];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self setSize:FALSE];
    [super viewDidAppear:animated];
}

I think you were close if you tried keeping a reference to the popoverController and used setPopoverContentSize:animated:

What works for me is this combination of setPopoverContentSize:animated: AND contentSizeForViewInPopover in that specific order, for each controller your put on the stack:

-(void)viewDidAppear:(BOOL)animated
{
    [self.popoverController setPopoverContentSize:whateverSize animated:true];
    self.contentSizeForViewInPopover = whateverSize;
    [super viewDidAppear:animated];
}

In this case, self.popoverController is a reference I keep in each controllers that is pushed on the stack, it would probably be cleaner to use a singleton variable for that.

If animation is what you seek, you can try the following piece of code. Honestly speaking, I have no experience in iPad development, but I use it to animate frame and alpha changes in almost all my projects. The Parameters are simple, you have, in order, the duration over which animation occurs, time delay, animation options, the block where you specify the changes in view, and another block object where you specify the aftermath.

[UIView animateWithDuration:1.0
                      delay:0.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
               self.contentSizeForViewInPopover = CGSizeMake(320, self.items.count * 44);
                 }
                 completion:^(BOOL finished){
                //Insert what you need to do after the animation is over
                 }];

You can try putting it in the -(void)viewDidAppear:(BOOL)animated

Try it out and let me know if it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!