Objective C 类 中的约束
Objective C Constraints in Classes
我目前正在尝试在 class 之上添加 UIIMageView
。
没有约束,我可以毫无阻碍地添加一个对象,例如UIImageView
:[self addSubview:myImage];
。
但是,当我添加 [myImage setTranslatesAutoresizingMaskIntoConstraints: NO];
以及所需的约束时,图像会添加到 UIViewController
而不是 class。
(现在,sizeX/Y 是一个单独的算法来设置图像的宽度和高度,如果约束有效,则不需要)
1) 为什么会这样?
2) 如何添加一个带约束的UIImageView
?
代码如下:
UIImageView *myImage = [[UIImageView alloc] init];
//AutoResize...
myImage.frame = CGRectMake(0, 0, sizeX, sizeY);
myImage.center = CGPointMake(placementX, placementY);
myImage.image = [UIImage imageNamed:@"customImage"];
[self addSubview:myImage];
// Width constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// Height constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
谢谢!
I'm currently attempting to add a UIMageView on top of a class...
...the image is added to the view controller versus the class.
这些说法对我来说都没有意义,但是...
查看您的约束代码,您为图像视图的宽度和高度设置了约束,但这不足以明确定位图像视图。您需要添加 2 个约束:用于图像视图的垂直和水平位置。例如,如果您希望图像视图在其父视图中居中:
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
我目前正在尝试在 class 之上添加 UIIMageView
。
没有约束,我可以毫无阻碍地添加一个对象,例如UIImageView
:[self addSubview:myImage];
。
但是,当我添加 [myImage setTranslatesAutoresizingMaskIntoConstraints: NO];
以及所需的约束时,图像会添加到 UIViewController
而不是 class。
(现在,sizeX/Y 是一个单独的算法来设置图像的宽度和高度,如果约束有效,则不需要)
1) 为什么会这样?
2) 如何添加一个带约束的UIImageView
?
代码如下:
UIImageView *myImage = [[UIImageView alloc] init];
//AutoResize...
myImage.frame = CGRectMake(0, 0, sizeX, sizeY);
myImage.center = CGPointMake(placementX, placementY);
myImage.image = [UIImage imageNamed:@"customImage"];
[self addSubview:myImage];
// Width constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// Height constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
谢谢!
I'm currently attempting to add a UIMageView on top of a class...
...the image is added to the view controller versus the class.
这些说法对我来说都没有意义,但是...
查看您的约束代码,您为图像视图的宽度和高度设置了约束,但这不足以明确定位图像视图。您需要添加 2 个约束:用于图像视图的垂直和水平位置。例如,如果您希望图像视图在其父视图中居中:
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];