2014-01-15 99 views
1

我的一個觀點控制器加載另一個與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

唉!我的眼睛!一點點的空白不會傷害。 – Abizern

+0

對不起,這段代碼正在進行中。 – septerr

回答

0
從這個職位的答案之一

找到解決方案: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中,設置超級vi EW的邊界,以保存關閉邊界:

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

如果部署目標是iOS的7,只需要設置self.preferredContentSize您的主視圖控制器。你可以在-viewDidLoad

佈局子視圖控制器的任何容器視圖的首選內容大小。

適用於iOS 7.0或更高版本。