问题
I use UIPopoverController to popover a window. I use preferredContentSize to set the size. Then, I use Push a new view controller with larger size. When the child view pop up, I like to recover the window to original size. But seems not work.
I already put the self.preferredContentSize = myWindowSize; But it seems can not recover.
In iOS6 or before, I set the contentSizeForViewInPopover to CGSizeZero, then set the proper size. But on iOS7, it will make the popup disappear.
回答1:
This solved it for me:
override func viewDidAppear(animated: Bool)
{
navigationController?.preferredContentSize = preferredContentSize
super.viewDidAppear(animated)
}
回答2:
The only solution I can think of is to dismiss your popover window and automatically present it again with the new size you want. I'm also having trouble for iOS 8 where the content size won't shrink in height. The width shrinks, but the height can only increase for me.
Have a look at
https://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverControllerDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIPopoverControllerDelegate/popoverController:willRepositionPopoverToRect:inView:
It might help you reposition the rect with the delegate protocol
Solution:
Ok, here's my solution. From the view controller where you present the popover controller, you can set up a delegate called something like "resetContentSize:(CGSize)size". e.g. in your presenting view controller's header file:
@protocol PopoverDelegate <NSObject>
- (void)resetContentSize:(CGSize)size
@end
in the implementation file where you present the popover controller, you can set the delegate
self.popoverController.delegate = self;
From your popover controller, you can add the PopoverDelegate in the header file
@property (nonatomic, weak) id<PopoverDelegate> popoverDelegate;
and call the delegate method in your implementation file:
[popoverDelegate resetContentSize:CGSizeMake(320.0f,205.0f)];
回答3:
Had the same issue. The solution for me was to set preferredContentSize on the navigationController, instead of self
.
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.preferredContentSize = myControllerSize
}
来源:https://stackoverflow.com/questions/18941875/preferredcontentsize-does-not-shrink