popover典型问题:Popover with embedded navigation controller doesn't respect size on back nav

QUESTION:

I have a UIPopoverController with navigationController and bunch of subviews. The size of the popover is set just before it's shown like this:

[self.myPopover setPopoverContentSize:CGSizeMake(320, 500)];

That works fine. The popover is shown with adjusted size. When another view is pushed on navigation stack the size of a popover is set again - need different height - in viewWillAppear method:

self.contentSizeForViewInPopover = CGSizeMake(320, 700);

This also works fine. When I go back to a previous view the size does not change.

I added the same call in viewWillAppear in first view but the view does not resize.

How can I manage resizing of popover when navigating between views?


ANSWER:

This is what I discovered: 

- When you set the contentSizeForViewInPopover in your view controller it won't be changed by the popover itself

 - even though popover size may change while navigating to different controller. 

- When the size of the popover will change while navigating to different controller, while going back, the size of the popover does not restore - Changing size of the popover in viewWillAppear gives very strange animation (when let's say you popController inside the popover) - I'd not recommend it

 - For me setting the hardcoded size inside the controller would not work at all - my controllers have to be sometimes big sometimes small - controller that will present them have the idea about the size though

A solution for all that pain is as follows: You have to reset the size of currentSetSizeForPopover inviewDidAppear. But you have to be careful, when you will set the same size as was already set in fieldcurrentSetSizeForPopover then the popover will not change the size. For this to happen, you can firstly set the fake size (which will be different than one which was set before) followed by setting the proper size. This solution will work even if your controller is nested inside the navigation controller and popover will change its size accordingly when you will navigate back between the controllers.

You could easily create category on UIViewController with the following helper method that would do the trick with setting the size:

- (void) forcePopoverSize {
    CGSize currentSetSizeForPopover = self.contentSizeForViewInPopover;
    CGSize fakeMomentarySize = CGSizeMake(currentSetSizeForPopover.width - 1.0f, currentSetSizeForPopover.height - 1.0f);
    self.contentSizeForViewInPopover = fakeMomentarySize;
    self.contentSizeForViewInPopover = currentSetSizeForPopover;
}

Then just invoke it in -viewDidAppear of desired controller.




- (void) forcePopoverSize {
    CGSize currentSetSizeForPopover = self.contentSizeForViewInPopover;
    CGSize fakeMomentarySize = CGSizeMake(currentSetSizeForPopover.width - 1.0f, currentSetSizeForPopover.height - 1.0f);
    self.contentSizeForViewInPopover = fakeMomentarySize;
    self.contentSizeForViewInPopover = currentSetSizeForPopover;
}

Then just invoke it in -viewDidAppear of desired controller.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值