如何在 Objective-C 中将约束添加到 UImageView



我正在开发一个应用程序,同时添加约束,我发现其中有错误。我在iPad 12.9和iPad 9.7中做。所以,我在12.9屏幕屏幕中设置了图像。这意味着在 Assests 文件夹中,我将两张图像存储在其中。

1.用于1366 * 1024-1x,2732 * 2048-2x的背景图像2.160 * 491-1X,321 * 985-2X的图像

那么如何在背景图像和图像上添加约束。我需要在正确的位置查看。

我已经在 12.9 屏幕尺寸上完成了。如何在 Xcode 中添加约束。请回复....

您可以按如下方式向图像视图添加约束:

   let imgView = UIImageView()
    imgView.translatesAutoresizingMaskIntoConstraints = NO;
 /* Leading space to superview */
    NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:imgView  attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute: NSLayoutAttributeLeft multiplier:1.0 constant:30];
    /* Top space to superview Y*/
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.imgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute: NSLayoutAttributeTop multiplier:1.0f constant:258];
    /* Trailing space to superview */
    NSLayoutConstraint *trailingConstraint = [NSLayoutConstraint constraintWithItem:imgView  attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute: NSLayoutAttributeRight multiplier:1.0 constant:30];
    /* Bottom space to superview Y*/
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.imgView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute: NSLayoutAttributeBottom multiplier:1.0f constant:258];
    /* 4. Add the constraints to button's superview*/
    [self addConstraints:@[leadingConstraint, topConstraint, trailingConstraint, bottomConstraint]];

最新更新