首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XAnchor/YAnchor的NSLayoutAnchor设置乘法器

XAnchor/YAnchor的NSLayoutAnchor设置乘法器
EN

Stack Overflow用户
提问于 2017-01-17 08:04:29
回答 2查看 1.8K关注 0票数 5

如何使用NSLayoutAnchor设定乘数值?

例如,,我们可以使用NSLayoutConstraint来完成乘数值为1.5的操作:

代码语言:javascript
复制
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];

使用NSLayoutXAxisAnchor时没有可用的乘法器参数。

代码语言:javascript
复制
   hConstraint = [view1.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor constant:0.0f];

只能指定常量值。

EN

回答 2

Stack Overflow用户

发布于 2017-01-17 10:20:13

NSLayoutAnchor方法有两个变体,它们接受乘数,但它们只能在NSLayoutDimension锚点(宽度和高度)上使用:

代码语言:javascript
复制
func constraint(equalTo anchor: NSLayoutDimension, multiplier m: CGFloat) -> NSLayoutConstraint
func constraint(equalTo anchor: NSLayoutDimension, multiplier m: CGFloat, constant c: CGFloat) -> NSLayoutConstraint

有关更多信息,请参阅这份文件

票数 0
EN

Stack Overflow用户

发布于 2019-04-05 06:13:55

如果我们只计算equalTo方法

  1. NSLayoutAnchor有两种方法
    • func约束(equalTo: NSLayoutAnchor) -> NSLayoutConstraint;
    • 函数约束(equalTo: NSLayoutAnchor,常数: CGFloat) -> NSLayoutConstraint;

这两种方法都是由NSLayoutAnchor的子类自动生成的。

2.nslayout维度有三个methods.This类是NSLayoutAnchor的子类。

  • 函数约束(equalTo: NSLayoutDimension,乘子: CGFloat) -> NSLayoutConstraint;
  • 函数约束(equalTo: NSLayoutDimension,乘子: CGFloat,常数: CGFloat) -> NSLayoutConstraint;
  • func约束(equalToConstant: CGFloat) -> NSLayoutConstraint;

3.NSLayoutXAxisAnchor只有一个相同的方法,这类是NSLayoutAnchor的子类

  • 函数约束(equalToSystemSpacingAfter: NSLayoutXAxisAnchor,乘子: CGFloat) -> NSLayoutConstraint;

4.因此,NSLayoutYAxisAnchor只有一个相同的方法,这个类是NSLayoutAnchor的子类。

  • 函数约束(equalToSystemSpacingAfter: NSLayoutYAxisAnchor,乘子: CGFloat) -> NSLayoutConstraint;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41691833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档