在Swift中以编程方式添加iOS约束

4

那么,我已经不再使用Xcode中的IB,希望能够用Swift编写所有的UI

所以我做的事情是:

  • Created a new UIView to contain the elements i want to write - lets call it "TestView"
  • I've added TestView to a VC as a sub view.
  • In the TestView class I've added the elements like this:

    class TestView: UIView {
          var someLabel:UILabel!
          override init(frame: CGRect) {
                super.init(frame: frame)
    
                self.someLabel = UILabel(frame: CGRect(x: self.frame.midX, y: oneSixthHeight, width: 100, height: 22))
                self.someLabel.text = "test"
    
                var constraints:[NSLayoutConstraint] = []
                self.someLabel.translatesAutoresizingMaskIntoConstraints = false
                let rightsideAnchor:NSLayoutConstraint = NSLayoutConstraint(item: self.someLabel, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 1)
    
                 constraints.append(rightsideAnchor)
                 NSLayoutConstraint.activateConstraints(constraints)
          }
    }
    
我希望这样就可以将UILabel锚定在视图的右侧。

然而,我遇到了以下错误:

未捕获异常“NSGenericException”,原因是“无法激活具有项目>和>的约束,因为它们没有共同的祖先。
是否约束引用了不同视图层次结构中的项目? 这是非法的。'

我做错了什么?


你把 someLabel 添加到哪个视图的子视图中了吗?只有当它们已经作为子视图添加时,才能应用约束并激活它们。否则尝试会导致崩溃,就像你刚才看到的那样。 - Sandeep Bhandari
2个回答

9

只有在视图添加到视图层次结构后才应添加约束。从您的代码中可以清楚地看出,您尚未将UILabel实例添加到视图中。


谢谢! :) 将标签添加为子视图,一切都像魔术般地运行。 <3 - Simon k

8

已更新至Swift 3

import UIKit

class ViewController: UIViewController {

let redView: UIView = {

    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .red
    return view
}()

override func viewDidLoad() {
    super.viewDidLoad()

    setupViews()
    setupAutoLayout()
}

func setupViews() {

    view.backgroundColor = .white
    view.addSubview(redView)
}

func setupAutoLayout() {

    // Available from iOS 9 commonly known as Anchoring System for AutoLayout...
    redView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
    redView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20).isActive = true

    redView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    redView.heightAnchor.constraint(equalToConstant: 300).isActive = true

    // You can also modified above last two lines as follows by commenting above & uncommenting below lines...
    // redView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
    // redView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
 }
}

在此输入图片描述

约束类型:

/*
// regular use
1.leftAnchor
2.rightAnchor
3.topAnchor
// intermediate use
4.widthAnchor
5.heightAnchor
6.bottomAnchor
7.centerXAnchor
8.centerYAnchor
// rare use
9.leadingAnchor
10.trailingAnchor
etc. (note: very project to project)
*/

谢谢,我总是忘记“view.translatesAutoresizingMaskIntoConstraints = false”。 - Pomo-J
9和10也应经常使用,并且不应该很少出现,特别是对于标签。有时语言是从右到左的。 - CyberMew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接