我将在iPad上呈现UINavigationController + UICollectionViewController作为表单,并希望它在横向和纵向方向上具有明确的大小。出于某种原因,我的方法无法在轮换后提供一致的结果。它似乎适用于两个方向的初始演示,以及从横向旋转到纵向时。从纵向旋转到横向打破它。
请注意我的目标是iOS 10+,因此很多现有的建议不再适用。
@implementation FormCVC
- (void)viewDidLoad {
...
[self setupFrame];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id _Nonnull context) {
[self setupFrame];
} completion:^(id _Nonnull context) {
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
[layout invalidateLayout];
}];
}
- (void)setupFrame {
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
NSLog(@"Portrait");
self.preferredContentSize = CGSizeMake(VIEW_PORTRAIT_WIDTH, VIEW_PORTRAIT_HEIGHT);
} else {
NSLog(@"Landscape");
self.preferredContentSize = CGSizeMake(VIEW_LANDSCAPE_WIDTH, VIEW_LANDSCAPE_HEIGHT);
}
[self.navigationController.presentationController.containerView setNeedsLayout];
[self.navigationController.presentationController.containerView layoutIfNeeded];
}
答案 0 :(得分:0)
解决方案实际上就像添加self.navigationController.preferredContentSize = ...
self.preferredContentSize = ...