使用presentViewController正确更改模态视图控制器的大小:animated:completion:

时间:2012-10-26 22:41:29

标签: objective-c ios5 resize ios6 presentmodalviewcontroller

在IOS 6之前,我使用下面的代码来呈现具有自定义大小的模态视图:

- (void) showModalViewWithController:(GUIViewController*) _viewController {
    [UIView beginAnimations:@"" context:nil];
    [self presentModalViewController:_viewController animated:YES];
    _viewController.view.superview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _viewController.view.superview.frame = CGRectMake(
        _viewController.view.superview.frame.origin.x,
        _viewController.view.superview.frame.origin.y,
        700.0f,
        self.view.frame.size.height - 80.f);
    _viewController.view.superview.center = self.view.center;

    [UIView commitAnimations];
}

现在我必须更改功能

- (void)presentModalViewController: (UIViewController *)viewControllerToPresent animated:(BOOL)flag

- (void)presentViewController: (UIViewController *)viewControllerToPresent animated: (BOOL)flag completion: (void (^)(void))completion

不幸的是,在更改之后调整大小不会很好:

- (void) showModalViewWithController:(GUIViewController*) _viewController {
    [self presentViewController:_viewController animated:YES completion:^(){
        [UIView beginAnimations:@"" context:nil];
        _viewController.view.superview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _viewController.view.superview.frame = CGRectMake(
            _viewController.view.superview.frame.origin.x,
            _viewController.view.superview.frame.origin.y,
            700.0f,
            self.view.frame.size.height - 80.f);
        _viewController.view.superview.center = self.view.center;
        [UIView commitAnimations];
    }];
}

因为模态视图在帧操作之前呈现,并且在调用完成块之后跳转。

如何处理?

提前致谢,

米哈尔

2 个答案:

答案 0 :(得分:0)

我不确定为什么完成块动画会跳转。我从未使用过[UIView beginAnimations:context:]方法。我通常称为基于块的动画方法,如[UIView animateWithDuration:animations:completion:]。这是因为在iOS 4及更高版本中不鼓励使用beginAnimations方法。

我会尝试更改动画以使用基于块的方法。提交动画后可能会出现一些奇怪的线程问题。

我发现在显示之后调整视图的大小非常有趣,而不是事先调整它的大小。

答案 1 :(得分:0)

AHKNavigationController * navigationController = [[AHKNavigationController alloc] initWithRootViewController:moreOverView];

[navigationController setModalPresentationStyle:UIModalPresentationFormSheet];

    navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    navigationController.navigationBarHidden=YES;
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
    {
        [self presentViewController:navigationController animated:YES completion:^{
            [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                    //Make the modal bigger than normal
                navigationController.view.superview.frame = CGRectMake(
                                                                       navigationController.view.superview.frame.origin.x,
                                                                       navigationController.view.superview.frame.origin.y,
                                                                       540,
                                                                       622
                                                                       );
            } completion:^(BOOL finished) {
            }];
        }];
    }
    else{
        [self presentViewController:navigationController animated:YES completion:nil];
        navigationController.preferredContentSize = CGSizeMake(540,622);
        navigationController.view.superview.center = self.view.center;
    }
    navigationController.view.superview.autoresizingMask =
    UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleBottomMargin;

https://github.com/fastred/AHKNavigationController

找到AHKNavigationController