从模态视图控制器中更改模态视图大小

Posted

技术标签:

【中文标题】从模态视图控制器中更改模态视图大小【英文标题】:Change Modal View Size From Within Modal View Controller 【发布时间】:2014-01-15 15:47:29 【问题描述】:

我的一个视图控制器以UIModalPresentationFormSheet 的呈现方式加载另一个视图控制器:

- (void)loadNotesForm 
if ([helper isOrderReadyForSubmission:self.coreDataOrder]) 
    CIFinalCustomerInfoViewController *ci = [[CIFinalCustomerInfoViewController alloc] init];
    ci.order = self.coreDataOrder;
    ci.modalPresentationStyle = UIModalPresentationFormSheet;
    ci.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    ci.delegate = self;
    [self presentViewController:ci animated:YES completion:nil];

在此模式 (CIFinalCustomerInfoViewController) 中,我以编程方式构造视图:

    - (void)loadView

    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    [view setBackgroundColor:[UIColor blackColor]];
    self.view = view;
    CGFloat currentY = 8.0;
    CGFloat verticalMargin = 8.0;
    CGFloat horizontalMargin = 12.0;
    UIFont *labelFont =  [UIFont fontWithName:@"Futura-MediumItalic" size:27.0f];
    UILabel *authorizedByLabel = [[UILabel alloc] initWithFrame:CGRectMake(62.0, currentY, 300.0, 35.0)];
    authorizedByLabel.font = labelFont;
    authorizedByLabel.textColor = [UIColor whiteColor];
    authorizedByLabel.text = @"Authorized By";
    [self.view addSubview:authorizedByLabel];
    self.authorizedByTextField = [[UITextField alloc] initWithFrame:CGRectMake(62.0, CGRectGetMaxY(authorizedByLabel.frame)+ verticalMargin, 419.0, 44.0)];
    self.authorizedByTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.authorizedByTextField];
    UILabel *notesLabel = [[UILabel alloc] initWithFrame:CGRectMake(62.0, CGRectGetMaxY(self.authorizedByTextField.frame) + verticalMargin, 300.0, 35.0)];
    notesLabel.font = labelFont;
    notesLabel.textColor = [UIColor whiteColor];
    notesLabel.text = @"Notes";
    [self.view addSubview:notesLabel];
    self.notesTextView = [[UITextView  alloc] initWithFrame:CGRectMake(62.0, CGRectGetMaxY(notesLabel.frame) + verticalMargin, 419.0, 100.0)];
    [self.view addSubview:self.notesTextView];
    UILabel *shipNotesLabel = [[UILabel alloc] initWithFrame:CGRectMake(62.0, CGRectGetMaxY(self.notesTextView.frame) + verticalMargin, 300.0, 35.0)];
    shipNotesLabel.font = labelFont;
    shipNotesLabel.textColor = [UIColor whiteColor];
    shipNotesLabel.text = @"Ship Notes";
    [self.view addSubview:shipNotesLabel];
    self.shipNotesTextView = [[UITextView  alloc] initWithFrame:CGRectMake(62.0, CGRectGetMaxY(shipNotesLabel.frame) + verticalMargin, 419.0, 80.0)];
    [self.view addSubview:self.shipNotesTextView];
    currentY = CGRectGetMaxY(self.shipNotesTextView.frame);
    if (self.contactBeforeShippingConfig) 
        UILabel *contactLabel = [[UILabel alloc] initWithFrame:CGRectMake(62.0, currentY, 300.0, 35.0)];
        contactLabel.font = labelFont;
        contactLabel.textColor = [UIColor whiteColor];
        contactLabel.text = @"Contact Before Shipping?";
        [self.view addSubview:contactLabel];
        self.contactBeforeShippingCB.frame = CGRectMake(62 + CGRectGetMaxX(contactLabel.frame) + horizontalMargin, contactLabel.frame.origin.y, 150, 35);
        [self.view addSubview:self.contactBeforeShippingCB];
        currentY = CGRectGetMaxY(self.contactBeforeShippingCB.frame);
    
    if(self.cancelConfig)
        UILabel *cancelLabel = [[UILabel alloc] initWithFrame:CGRectMake(62.0, currentY, 300.0, 35.0)];
        cancelLabel.font = labelFont;
        cancelLabel.textColor = [UIColor whiteColor];
        cancelLabel.text = @"Cancel if not shipped within following days?";
        [self.view addSubview:cancelLabel];
        self.cancelBeforeDaysPicker.frame = CGRectMake(62, CGRectGetMaxY(cancelLabel.frame), 400, 100);
        [self.view addSubview:self.cancelBeforeDaysPicker];
        currentY = CGRectGetMaxY(self.cancelBeforeDaysPicker.frame);
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchDown];
    [cancelButton setBackgroundImage:[UIImage imageNamed:@"cart-cancelout.png"] forState:UIControlStateNormal];
    [cancelButton setBackgroundImage:[UIImage imageNamed:@"cart-cancelin.png"] forState:UIControlStateHighlighted];
    cancelButton.frame = CGRectMake(62.0, currentY+verticalMargin, 162.0, 56.0);
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchDown];
    [submitButton setBackgroundImage:[UIImage imageNamed:@"submitorderout.png"] forState:UIControlStateNormal];
    [submitButton setBackgroundImage:[UIImage imageNamed:@"submitorderin.png"] forState:UIControlStateSelected];
    submitButton.frame = CGRectMake(cancelButton.frame.origin.x + cancelButton.frame.size.width + horizontalMargin , cancelButton.frame.origin.y, 260.0, 56.0);
    currentY = CGRectGetMaxY(submitButton.frame);
    [self.view addSubview:cancelButton];
    [self.view addSubview:submitButton];

我希望调整模态框的高度,以适应在 loadView 方法中添加的内容。如果我从加载视图中设置帧大小,那是行不通的。 SO上的其他帖子建议从显示它的控制器更改模态的大小并且有效。但是显示它的控制器不知道什么高度会覆盖由模态的loadView 方法加载的内容。

有没有办法使模态框的高度适合其在模态框内或其父级中的内容?

【问题讨论】:

啊!我的眼睛!一点空白不会有什么坏处。 抱歉,此代码正在进行中。 【参考方案1】:

从这篇文章的答案之一中找到解决方案:iPad custom size of modal view controller

在我的模态视图控制器的loadView 中,我设置了self.view 的帧大小以适应内容:

- (void)loadView
//add ui elements and calculate max total height for the view (i.e. currentY below)
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, 540, currentY);

viewDidLoad 中,保存视图的边界:

- (void)viewDidLoad 
    [super viewDidLoad];
    originalBounds = self.view.bounds;

viewWillAppear 中,将超级视图的边界设置为保存的边界:

- (void)viewWillAppear:(BOOL)animated 
    [super viewWillAppear:animated];
    self.view.superview.bounds = originalBounds;

【讨论】:

【参考方案2】:

如果您的部署目标来自 ios 7,只需为您的主视图控制器设置 self.preferredContentSize。您可以在-viewDidLoad 中进行操作。

布局子视图控制器的任何容器视图的首选内容大小。

适用于 iOS 7.0 及更高版本。

【讨论】:

以上是关于从模态视图控制器中更改模态视图大小的主要内容,如果未能解决你的问题,请参考以下文章

iOS——如何控制模态视图控制器的大小?

从模态视图控制器 ios7 更改选项卡栏应用程序的选定索引

如何通知父视图控制器模态视图控制器中更改的屏幕方向?

模态视图控制器大小

在模态视图控制器后面更改视图控制器

更改模态视图控制器的cornerRadius