我已将自己的自定义uiview添加到self.view。由于在这种情况下我们无法在故事板中设置约束条件,因此我尝试以编程方式添加。为uiview设置编程方式的约束以支持纵向/横向
关注此How to Create layout constraints programmatically。
我正在使用下面的代码。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[customView setNeedsUpdateConstraints];
}
-(void)updateViewConstraints
{
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[self.view addConstraints:@[
//view1 constraints
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:padding.top],
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:padding.left],
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-padding.bottom],
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1
constant:-padding.right],
]];
}
我打电话从viewDidLoad
[self updateViewConstraints]
,但还是我得到的景观一半视图。
对此有任何想法。提前致谢。
如果您未向我们展示您添加的约束条件,我们如何提供答案? – Lefteris
@Lefteris我添加了使用相同约束的链接。 –