2017-09-02 124 views
-1

我是一位新手,以编程方式计算出自动布局。Autolayout不会垂直添加间隙

我定义了一个imageview,添加到superview(UIViewController)。使用视觉格式添加约束来设置差距。我没有提供价值只是破折号( - )。根据资源iam提到学习,没有提供价值,我会得到8pts的差距。

我水平但不垂直地得到空白。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
UIImageView * thumbnailImageView = [[UIImageView alloc] init]; 
thumbnailImageView.backgroundColor = [UIColor greenColor]; 
thumbnailImageView.image = [UIImage imageNamed:@"thumbnailImage"]; 
thumbnailImageView.contentMode = UIViewContentModeScaleAspectFill; 
thumbnailImageView.layer.masksToBounds = true; 
thumbnailImageView.translatesAutoresizingMaskIntoConstraints = false; 

[self.view addSubview:thumbnailImageView]; 

[self.view addConstraints:[NSLayoutConstraint 
         constraintsWithVisualFormat:@"V:|-[thumbnailImageView]-|" options:0 
         metrics:nil 
         views:NSDictionaryOfVariableBindings(thumbnailImageView)]]; 
[self.view addConstraints:[NSLayoutConstraint 
          constraintsWithVisualFormat:@"H:|-[thumbnailImageView]-|" options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(thumbnailImageView)]]; 

enter image description here

回答

1

如果你想一直使用的空间,你需要设置:self.view.preservesSuperviewLayoutMargins = YES;

或者,您也可以更改页边距,以及:

self.view.layoutMargins = UIEdgeInsetsMake(8, 8, 8, 8); 
self.view.preservesSuperviewLayoutMargins = YES; 

否则视图会相应地增长或缩小以适应。当你添加上面的代码行时,你会注意到它默认为你添加了8个像素到所有的约束。但是,水平约束可能现在看起来更大或看起来像是16 ..这是因为边距可能是8,16或20.

明确指定大小总是更好。

+0

谢谢帮助 – ios