NSLayoutAnchor - 设置 XAnchor/YAnchor 的乘数

use*_*205 5 iphone uikit ios

如何使用设置乘数值NSLayoutAnchor

例如,我们可以使用 NSLayoutConstraint 来做到这一点,乘数值为 1.5:

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = false;
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
NSLayoutConstraint *hConstraint = [NSLayoutConstraint constraintWithItem:view1
                                                              attribute:NSLayoutAttributeCenterY
                                                              relatedBy:NSLayoutRelationEqual toItem:self.view
                                                            attribute:NSLayoutAttributeCenterY
                                                             multiplier:1.5 constant:0.0f];
Run Code Online (Sandbox Code Playgroud)

使用时没有可用的乘数参数 NSLayoutXAxisAnchor

   hConstraint = [view1.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor constant:0.0f];
Run Code Online (Sandbox Code Playgroud)

只能指定常数值。

Gia*_*one 0

该方法有两种NSLayoutAnchor接受乘数的变体,但它们仅适用于NSLayoutDimension锚点(宽度和高度):

func constraint(equalTo anchor: NSLayoutDimension, multiplier m: CGFloat) -> NSLayoutConstraint
func constraint(equalTo anchor: NSLayoutDimension, multiplier m: CGFloat, constant c: CGFloat) -> NSLayoutConstraint
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此文档