使用UIModalPresentationCustom在ModalViewController中设置框架iOS8

时间:2014-11-14 18:31:18

标签: ios ios8 modalviewcontroller presentmodalviewcontroller

我想提出一个模态视图控制器。在iOS7中,一切运行良好,但在iOS8中,视图控制器的框架已更改。我读了一些答案,其中一个解决方案是设置preferredContentSize和modalPresentationStyle = UIModalPresentationFormSheet。

但我需要我的modalPresentationStyle = UIModalPresentationCustom,我不能用这种演示风格设置视图控制器的框架。

我的代码是:

- (void)presentViewController:(UIViewController*)viewController withCustomSize:(NSValue*) size withInitialSetup:(void (^)(void))setupBlock withCompletion:(void (^)(void))completion {

    UiViewController * navCon = [[UIViewController alloc] initWithRootViewController:viewController];
    navCon.shouldDismissKeyboardOnResign = YES;

    if (SYSTEM_VERSION_LESS_THAN(IOS8)){

        navCon.modalPresentationStyle   = UIModalPresentationFormSheet;
        navCon.modalTransitionStyle     = UIModalTransitionStyleCoverVertical;


    } else {

        navCon.modalPresentationStyle   = UIModalPresentationCustom;
        navCon.modalTransitionStyle     = UIModalTransitionStyleCrossDissolve;

    }

    [viewController setupCustomNavigationBar];

    setupBlock();

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS8)) {

        navCon.transitioningDelegate = self;

        if (size) {

            CGSize sz = size.CGSizeValue;
            navCon.preferredContentSize = CGSizeMake(sz.width, sz.height);

        }else {

            navCon.preferredContentSize = CGSizeMake(kPopupsWidth, kPopupsHeight);
        }
    }

    [self presentViewController:navCon animated:YES completion:completion];

    if (SYSTEM_VERSION_LESS_THAN(IOS8)) {

        if (size) {

            CGSize sz = size.CGSizeValue;
            navCon.view.superview.bounds = CGRectMake(0, 0, sz.width, sz.height);

        } else {

            navCon.view.superview.bounds = CGRectMake(0, 0, kPopupsWidth, kPopupsHeight);
        }

        [navCon.view.superview.layer setCornerRadius: 8];
        [navCon.view.superview.layer setBorderColor: [UIColor clearColor].CGColor];
        [navCon.view.superview.layer setBorderWidth: 2];
        [navCon.view.superview setClipsToBounds: YES];   
    }
}

我设置了preferedContentSize,但框架没有改变。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

如果您要使用UIModalPrestationCustom,则必须提供UIViewControllerTransitioningDelegate,否则将使用默认的转换委托,在您的情况下听起来像表单委托。< / p>