为什么我的模态视图的尺寸与我的预期不同?

时间:2013-09-14 16:38:03

标签: ios interface-builder modal-dialog viewcontroller popover

我在IB中创建了这个对话框: dialog

我按以下方式设置了一个segue: segue

这是我配置ViewController的方式: ViewController

这就是我配置视图指标的方式: View Metrics

这是我在iOS模拟器中得到的: enter image description here

所以:有人可以告诉我为什么,我怎么能按照我的视觉方式得到它,以及在哪里可以找到一个非常好的界面构建器教程?

提前致谢。

1 个答案:

答案 0 :(得分:2)

“表单”模式表示样式始终使用相同大小的容器,您无法覆盖它。使用iOS 7或更高版本,您可以定义自定义模式演示文稿样式以完成您想要的操作,或搜索GitHub。

这是一个与自定义尺寸相关的iOS 6兼容实现。

/** Created in answer to mirx's question at http://stackoverflow.com/questions/18803961/why-are-the-dimensions-of-my-modal-view-different-from-what-i-expect/18805374?noredirect=1#comment27741546_18805374 */

@interface AHPresentingViewController : UIViewController
@property (nonatomic, weak) UIView *presentedBackgroundView;
@property (nonatomic, weak) UIViewController *formController;
@end

@implementation AHPresentingViewController

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UITableViewController *table = [UITableViewController new];
    table.title = @"Form";
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:table];
    table.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissPresentedForm:)];
    [self presentForm:nav size:CGSizeMake(320, 240)];
}
-(void)presentForm:(UIViewController *)formVC size:(CGSize)size {
    if(self.formController) return;
    NSLog(@"Form view: %@",formVC.view);
    UIView *background;
    self.presentedBackgroundView = background = [[UIView alloc] initWithFrame:self.view.bounds];
    background.opaque = NO;
    background.backgroundColor = [UIColor blackColor];
    background.alpha = 0;
    [self.view addSubview:background];
    formVC.view.frame = (CGRect){CGPointZero, size};
    formVC.view.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2., CGRectGetHeight(self.view.bounds) + CGRectGetHeight(formVC.view.bounds));
    [self addChildViewController:formVC];
    self.formController = formVC;
    [self.view addSubview:formVC.view];

    [UIView animateWithDuration:0.5 animations:^{
        background.alpha = 0.4;
        formVC.view.center = CGPointMake(self.view.bounds.size.width / 2., self.view.bounds.size.height / 2.);
    }];
}
-(void)dismissPresentedForm:(id)sender {
    if(!self.formController) return;
    [UIView animateWithDuration:0.5 animations:^{
        self.presentedBackgroundView.alpha = 0;
        self.formController.view.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2., CGRectGetHeight(self.view.bounds) + CGRectGetHeight(self.formController.view.bounds));
    } completion:^(BOOL finished) {
        [self.presentedBackgroundView removeFromSuperview];
        [self.formController.view removeFromSuperview];
        [self.formController removeFromParentViewController];
    }];
}
@end