【问题标题】:Resize ModalViewController and position it at center in iOS 7调整 ModalViewController 的大小并将其定位在 iOS 7 的中心
【发布时间】:2013-10-21 11:56:49
【问题描述】:

我试图通过减小其宽度和高度来在 iPad 上显示 modalView,但问题是它没有居中对齐。在 iOS 6 中它曾经可以正常工作,但在 iOS 7 中它不是居中对齐的。

下面是我的代码:

 m_helpQA = [[HelpQAViewController alloc]init];

 m_helpQA.modalPresentationStyle = UIModalPresentationFormSheet;      

 [self presentViewController:m_helpQA animated:YES completion:NULL];
 m_helpQA.view.superview.bounds = CGRectMake(0, 0, 350, 250);//Dimensions of ModalView.

目前我是这样理解的

【问题讨论】:

    标签: ipad ios7 modalviewcontroller presentmodalviewcontroller


    【解决方案1】:

    对于 iOS 7 试试这个:

    [self.navigationController presentViewController:navigationController animated:YES completion:^{
        //Make the modal bigger than normal
        navigationController.view.superview.bounds = CGRectMake(0, 0, 700, 650);
    }];
    

    动画看起来很难看,所以我建议添加一个动画来改进它:

    [self.navigationController presentViewController:navigationController animated:YES completion:^{
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            //Make the modal bigger than normal
            navigationController.view.superview.bounds = CGRectMake(0, 0, 700, 650);
        } completion:^(BOOL finished) {
        }];
    }];
    

    还请记住,您需要在 viewDidAppear 中设置 navigationControllers 视图的框架,以使内容的大小正确。

    【讨论】:

    • 这个问题最初是针对 iOS7 提出的,关于大小,但我发现它对位置 (frame.origin) 也很有用。但是,至少对于位置而言,这不再适用于 iOS8。任何想法>
    【解决方案2】:

    您需要将框架居中,这样应该可以工作(我还没有测试过)。

    CGRect appFrame = [UIScreen mainScreen].applicationFrame;
    CGRect modalFrame = m_helpQA.view.superview.frame;
    modalFrame.size = CGSizeMake(350.f, 250.f);
    modalFrame.origin.x = appFrame.size.width/2.0f - modalFrame.size.width/2.0f;
    modalFrame.origin.y = appFrame.size.height/2.0f - modalFrame.size.height/2.0f;
    m_helpQA.view.superview.frame = modalFrame;
    

    更新代码以纠正错字

    【讨论】:

    • 嘿,我试过了,它在中心,但我无法降低 modalView 的高度。
    【解决方案3】:

    尝试以下方法:

    UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    ...
    
    [self presentViewController:vc animated:NO completion:^{
        vc.view.superview.center = self.view.window.center;
    }];
    
    // resize modal view
    infoModalViewController.view.superview.bounds = CGRectMake(0, 0, newWidth, newHeight);
    

    我必须在完成块中设置中心(在本例中为窗口的中心,但您也可以使用父视图控制器的中心)以使其正常工作。切换到animation:No,否则它会看起来很丑:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-23
      • 2014-04-14
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      相关资源
      最近更新 更多