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

感謝您的代碼。但是我仍然在旋轉之後得到了半屏:( –

相關問題