我的一个视图控制器加载另一个视图样式为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
方法加载的内容。
有没有办法让模态的高度适合模态或其父模式中的内容?
答案 0 :(得分: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
中,将超视图的界限设置为已保存的非界限:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.view.superview.bounds = originalBounds;
}
答案 1 :(得分:0)
如果您的部署目标来自iOS 7,则只需为主视图控制器设置self.preferredContentSize
即可。您可以在-viewDidLoad
。
布局子视图控制器的任何容器视图的首选内容大小。
适用于iOS 7.0及更高版本。