NSLayoutConstraint在swift中 [英] NSLayoutConstraint in swift

查看:128
本文介绍了NSLayoutConstraint在swift中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下代码转换为Swift:

I am trying to convert the following code to Swift:

leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
                                              attribute:NSLayoutAttributeLeft
                                              relatedBy:0
                                                 toItem:self.view
                                              attribute:NSLayoutAttributeLeft
                                             multiplier:1.0
                                               constant:0];
[self.view addConstraint:leftConstraint];

有人可以在Swift中为我提供新语法吗?

Can someone give me the new syntax to do it in Swift?

推荐答案

复制&粘贴来自 documentation

Copy & paste from the documentation:

convenience init(item view1: AnyObject!,
            attribute attr1: NSLayoutAttribute,
         relatedBy relation: NSLayoutRelation,
               toItem view2: AnyObject!,
            attribute attr2: NSLayoutAttribute,
      multiplier multiplier: CGFloat,
                 constant c: CGFloat)

所以你的代码转换为

let leftConstraint = NSLayoutConstraint(item: self.contentView, 
                                   attribute: .left, 
                                   relatedBy: .equal,
                                      toItem: self.view,
                                   attribute: .left, 
                                  multiplier: 1.0, 
                                    constant: 0.0);
self.view.addConstraint(leftConstraint);

为Swift 4更新的代码。

这篇关于NSLayoutConstraint在swift中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆