0

enter image description here我已将自己的自定义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],但还是我得到的景观一半视图。

对此有任何想法。提前致谢。

+0

如果您未向我们展示您添加的约束条件,我们如何提供答案? – Lefteris

+0

@Lefteris我添加了使用相同约束的链接。 –

回答

0

您可以使用Masonry来达到此目的。例如:

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10); 

[view1 mas_makeConstraints:^(MASConstraintMaker *make) { 
make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler 
make.left.equalTo(superview.mas_left).with.offset(padding.left); 
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom); 
make.right.equalTo(superview.mas_right).with.offset(-padding.right);}]; 

这将为您的视图设置顶部,右侧,底部和左侧约束。

+0

感谢您的回复。我按照你的建议,但仍然旋转后我得到了一半的屏幕。我在viewDidLoad中添加了这段代码。我需要添加任何? –

+0

@SteveGear尝试将其添加到viewDidAppear中,因为在viewDidLoad视图superview没有设置 – ko7tya

+0

不工作ko7tya..uploaded图像检查 –

0

尝试在willRotateToInterfaceOrientation:duration:请致电setNeedsUpdateConstraints查看需要更新其约束。

除非观点并没有通知它不会更新约束

0

我你的约束是否设置正确,视图将根据他们当用户改变方向调整。 如果您向我们显示您的代码,我们将更容易为您提供帮助。

一些提示:

  • 务必将translatesAutoresizingMaskIntoConstraints属性设置为NO
  • 您可以使用可视格式设置你的限制,有时更容易,不要忘记一个:Visual Format Language
  • 检查您的日志以查看约束是否不中断

如果您的约束设置正确,应该调整视图的大小accor直到它的超级框架。

UPDATE

我看到两种可能性:要么限制是不是真的设置(因为视图不会例如准备),无论是一个约束或多个被破坏(但你应该看到,一些日志案件)。

我给你一些代码对我的作品(我个人使用Visual Format的约束如果可能):

customView = [UIView new]; 
customView.translatesAutoresizingMaskIntoConstraints = NO; 
[self addSubview:customView]; 

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10); 

NSDictionary *views = @{ @"customView" : customView }; 
NSDictionary *metrics = @{ @"top" : @(padding.top) , @"bottom" : @(padding.bottom) , @"right" : @(padding.right) , @"left" : @(padding.left) }; 
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[customView]-right-|" options:0 metrics:nil views:views]]; 
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[customView]-bottom-|" options:0 metrics:nil views:views]]; 

我不使用的方法[customView setNeedsUpdateConstraints];旋转时有发生:系统更新的限制子视图本身。

+0

另一个可能的raisons:确保您的视图已添加在您设置的约束之前。更重要的是,文档告诉我们使用方法'[NSLayoutConstraint activateConstraints:]'而不是'[view addConstraints:]':[Apple doc](https://developer.apple.com/reference/uikit/uiview/ 1622513-addconstraints?language = objc) –

+0

两者都是一样的@Olivier –

+0

感谢您的代码。但是我仍然在旋转之后得到了半屏:( –

相关问题