我在iPad上的mainviewcontroller中呈现了一个类似popup的viewcontroller,效果很好。但是目前我正在设置它的prefferedContentSize以适应屏幕,因此当我旋转时,尺寸不会自动调整。我需要将popupview与mainviewcontroller中心对齐,纵向和横向的宽度为97%,高度为90%。所以我现在尝试在我的popupViewController上添加autolayout约束,但不知道如何。下面是我到目前为止尝试过的代码。这给了我一个' NSInternalInconsistencyException '错误
#import "ViewController.h"
@interface ViewController ()
{
PopUpViewController *popUpController;
}
@end
- (IBAction)showPopUp:(UIButton *)sender {
popUpController = [[PopUpViewController alloc]initWithNibName:@"PopUpViewController" bundle:nil];
popUpController.view.center = self.view.center;
popUpController.view.layer.cornerRadius = 8.0f;
popUpController.view.layer.masksToBounds = YES;
popUpController.view.layer.borderWidth = 1.0f;
popUpController.view.layer.borderColor = [UIColor blueColor].CGColor;
popUpController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
popUpController.modalPresentationStyle = UIModalPresentationFormSheet;
// CGPoint frameSize = CGPointMake([[UIScreen mainScreen] bounds].size.width*0.97f, [[UIScreen mainScreen] bounds].size.height*0.9f);
// popUpController.preferredContentSize = CGSizeMake(frameSize.x, frameSize.y);
[self addConstraint];
[self.navigationController presentViewController:popUpController animated:YES completion:nil];
}
-(void)addConstraint
{
// Width constraint, half of parent view width
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:0.97
constant:0]];
// Height constraint, half of parent view height
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.9
constant:0]];
// Center horizontally
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
}
答案 0 :(得分:0)
在故事板中,将约束链接到控制器属性。然后,更改约束值:self.myConstraint.constant = 0;
。
此外,在更改约束值后,在视图上调用layoutIfNeeded
,在UIAnimation中执行此操作。
管理约束的最简单方法。