ios - 为什么我的约束不起作用? iOS系统

标签 ios nslayoutconstraint

我只是想在我的 imgView 上设置宽度和高度约束。我有以下代码:

UIImageView *imgView = [[UIImageView alloc] init];
imgView.center = self.view.center;
imgView.image = [UIImage imageNamed:@"favorites5-highlighted.png"];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview:imgView];

// width constraint
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imgView(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(imgView)]];

// height constraint
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[imgView(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(imgView)]];

我最终收到此错误:无法同时满足约束。 我查了一下并尝试添加:

imgView.translatesAutoresizingMaskIntoConstraints = NO;

self.view.translatesAutoresizingMaskIntoConstraints = NO;

编辑:

我只是想将 imageView 的宽度和高度设置为所需的属性。我首先尝试使用

CGRect rect = CGRectMake(0, 0, 10, 10);
imgView.frame = rect;

但没有任何改变

最佳答案

两个问题:

  1. 你确实想要:

    imgView.translatesAutoresizingMaskIntoConstraints = NO;
    

    你不想要:

    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    
  2. 您设置center的尝试是徒劳的,因为当应用约束时,它将会丢失。不幸的是,您没有定义约束来定义 ImageView 的位置。您可以使用下面 block 的最后两行来执行此操作:

    UIImageView *imgView = [[UIImageView alloc] init];
    //imgView.center = self.view.center;                                // this does nothing in auto layout
    imgView.image = [UIImage imageNamed:@"favorites5-highlighted.png"];
    imgView.contentMode = UIViewContentModeCenter;
    imgView.translatesAutoresizingMaskIntoConstraints = NO;             // add this line
    [self.view addSubview:imgView];
    
    NSDictionary *views = NSDictionaryOfVariableBindings(imgView);
    
    // width constraint
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imgView(100)]" options:0 metrics:nil views:views]];
    
    // height constraint
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[imgView(100)]" options:0 metrics:nil views:views]];
    
    // center align
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:imgView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:imgView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    

关于ios - 为什么我的约束不起作用? iOS系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755862/

相关文章:

ios - 为什么模态呈现 View Controller 会破坏我的 TableView 中的自动布局?

ios - 按该数组中的日期对 Swift 4 中的 Tableviews 部分进行排序

android - Android中Whatsapp的网址架构

iphone - 如何在 iTunes Connect 中提交自动续订订阅 + 应用程序以供审核?

ios - 无法满足约束时,如何识别 Debug 区域中的 View ?

ios - Xcode 7 : Constraints are disabled for subview

ios - 在 iOS 中使用 SSL 连接到 Twitter API

ios - 应用已获批准但未显示 AdMob 广告

非 sibling 之间的 iOS Autolayout 约束

ios - IB可设计渲染NSLayoutConstraint子类