在方向更改后,呈现的控制器位置会在iOS 8中更改

时间:2015-02-19 10:34:19

标签: ipad uiviewcontroller ios8 uiinterfaceorientation

我正在另一个UIViewController上实现UIViewController(模态)的演示。我希望presentController位于特定的Y位置,而不是父控制器的中心。这个Y位置在纵向和横向上都不同。在方向改变后,控制器已经带回中心。这是我尝试过的一段代码..

目标设备操作系统版本:iOS 8.不使用autolayout,因为它是现有的代码库。

- (IBAction)PresentControllerSelected:(id)sender
{

    PresentedViewController *vcPresented = [[PresentedViewController alloc]initWithNibName:@"PresentedViewController" bundle:nil];
    vcPresented.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vcPresented.modalPresentationStyle = UIModalPresentationFormSheet;


    vcPresented.preferredContentSize = CGSizeMake(500, 250);

    [self presentViewController:vcPresented animated:NO completion:^{


    }];

    CGRect r;
    if(UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
    {
        r = CGRectMake(self.view.bounds.size.width/2 - (500/2),
                       self.view.bounds.size.height/2 - (250/2) + 100,
                       500, 250);

    }
    else{
        r = CGRectMake(self.view.bounds.size.width/2 - (500/2),
                       self.view.bounds.size.height/2 - (250/2) - 100,
                       500, 250);

    }

    vcPresented.view.superview.frame = r;
    vcPresented.view.superview.backgroundColor = [UIColor clearColor];
}

在呈现视图控制器中,如果我添加方向更改委托方法,则不更新呈现的控制器框架。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];


    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        CGRect r;
        if(UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
        {
            r = CGRectMake(self.view.bounds.size.width/2 - (500/2),
                           self.view.bounds.size.height/2 - (250/2) + 100,
                           500, 250);

        }
        else{
            r = CGRectMake(self.view.bounds.size.width/2 - (500/2),
                           self.view.bounds.size.height/2 - (250/2) - 100,
                           500, 250);
        }

        self.presentedViewController.view.superview.center = CGPointMake(r.origin.x+r.size.width/2, r.origin.y+r.size.height/2);

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        // Do Nothing
    }];
}

这里是presentController类:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillLayoutSubviews{

    [super viewWillLayoutSubviews];

    CGRect r = self.view.frame;
    self.view.frame = r;

    if(UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
    {
        r = CGRectMake(self.presentingViewController.view.bounds.size.width/2 - (500/2),
                       self.presentingViewController.view.bounds.size.height/2 - (250/2) + 100,
                       500, 250);

    }
    else{
        r = CGRectMake(self.presentingViewController.view.bounds.size.width/2 - (500/2),
                       self.presentingViewController.view.bounds.size.height/2 - (250/2) - 100,
                       500, 250);

    }
    self.view.superview.center = CGPointMake(r.origin.x+r.size.width/2, r.origin.y+r.size.height/2);
}

0 个答案:

没有答案