旋转时的IPad模态形状尺寸问题

时间:2017-09-27 08:06:09

标签: ios objective-c xcode

我将在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];
}

1 个答案:

答案 0 :(得分:0)

解决方案实际上就像添加self.navigationController.preferredContentSize = ...

一样简单 每次

self.preferredContentSize = ...