【问题标题】:Programmatically using constraints to center a UIIMageView以编程方式使用约束使 UIIMageView 居中
【发布时间】:2015-06-16 15:54:35
【问题描述】:

我正在开发一个 iOS 项目,在该项目中我需要以编程方式为我的视图使用约束。我习惯了故事板,但由于项目规范的性质,我在这个特定项目中使用 xibs。

我有一个MainViewController,我在其中的 .h 文件中创建了以下属性:

@property (nonatomic, strong) IBOutlet UIImageView *backgroundImageView;
@property (nonatomic, strong) IBOutlet UIImageView *logoImage;

我将这些 UIImageView 实例添加到我的 XIB 文件中,并通过属性检查器选择了适当的图像。

在我的 .m 文件中,我有一个 addConstraints 方法,在 viewDidLoad 中调用。在这个方法中,我确保关闭将 autoresizingMasks 转换为约束:

self.backgroundImageView.translatesAutoresizingMaskIntoConstraints = NO;
self.logoImage.translatesAutoresizingMaskIntoConstraints = NO;

然后,我设置了约束,以便背景图片占据整个超级视图:

id mainView = @{@"background": self.backgroundImageView};

//Set constraints so that the background takes up the entirety of the superview
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[background]|" options:0 metrics:nil views:mainView]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[background]|" options:0 metrics:nil views:mainView]];

最后,我设置了约束,使徽标视图位于视图的中心(这是我出错的地方):

// Width constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeWidth
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeWidth
                                                multiplier:0.5
                                                  constant:0]];

// Height constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeHeight
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeHeight
                                                multiplier:0.5
                                                  constant:0]];

// Center horizontally
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:0.0]];

// Center vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeCenterY
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeCenterY
                                                multiplier:1.0
                                                  constant:0.0]];

我收到的错误是

*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: 每个约束项都必须是 UIView 或子类的实例'

,我不完全理解,因为我的约束项(两个UIImageView 实例)是UIView 的子类,但我可能误解了这一点。谁能帮忙指出我哪里出错了?

【问题讨论】:

    标签: ios objective-c uiimageview autolayout xib


    【解决方案1】:

    试试看:

    // Width constraint
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                     attribute:NSLayoutAttributeWidth
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.backgroundImageView
                                                     attribute:NSLayoutAttributeWidth
                                                    multiplier:0.5
                                                      constant:0]];
    
    // Height constraint
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                     attribute:NSLayoutAttributeHeight
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.backgroundImageView
                                                     attribute:NSLayoutAttributeHeight
                                                    multiplier:0.5
                                                      constant:0]];
    
    // Center horizontally
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                     attribute:NSLayoutAttributeCenterX
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.backgroundImageView
                                                     attribute:NSLayoutAttributeCenterX
                                                    multiplier:1.0
                                                      constant:0.0]];
    
    // Center vertically
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                     attribute:NSLayoutAttributeCenterY
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.backgroundImageView
                                                     attribute:NSLayoutAttributeCenterY
                                                    multiplier:1.0
                                                      constant:0.0]];
    

    您试图在 UIImageView 和视图控制器之间定义约束。您必须在作为同一视图的子视图的视图之间定义约束。

    【讨论】:

    • 啊!谢谢你。有时候,有另一双眼睛真的很好!
    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2015-05-25
    • 1970-01-01
    相关资源
    最近更新 更多