Stackview标签不以编程方式添加

时间:2016-11-23 22:45:15

标签: swift

我正在尝试在没有故事板的情况下做所有事情,但在添加我的标签时一直都是零,但我不明白为什么。我确定它死了很简单而且我是盲人但是我在谷歌搜索后却看不到它。

查看控制器:

    class SquaresViewController: UIViewController {

        let stackView = OAStackView()
        let titleView = TitleView(frame: CGRect.zero)

        @IBOutlet var squaresCollectionView: UICollectionView!

        override func viewDidLoad() {
            super.viewDidLoad()

            stackView.axis = .vertical
            stackView.translatesAutoresizingMaskIntoConstraints = false

            stackView.addArrangedSubview(titleView)
            titleView.snp.makeConstraints { make in
                make.height.equalTo(50)
                make.top.equalTo(view.snp.top)
                make.left.equalTo(view.snp.left)
                make.right.equalTo(view.snp.right)
            }

        }
    }

titleview的:

    import UIKit

    class TitleView: UIView {

        @IBOutlet weak var titleText: UILabel!

        override init(frame: CGRect) {
            super.init(frame: frame)
            self.translatesAutoresizingMaskIntoConstraints = false
            createView()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        func createView() {
            titleText = UILabel(frame: CGRect.zero)
            titleText.translatesAutoresizingMaskIntoConstraints = false

            titleText.text = "Tappy tap tap a square!"
            titleText.textAlignment = .center
            titleText.numberOfLines = 0

            self.addSubview(titleText)

            titleText.snp.makeConstraints { make in
                make.edges.equalTo( self.snp.edges )
            }
        }
    }

构建时失败:

    titleText.translatesAutoresizingMaskIntoConstraints = false

使用:

  

致命错误:在解包可选值时意外发现nil

1 个答案:

答案 0 :(得分:1)

首先,如果你不想使用故事板,我不明白为什么你为titleText创建了IBOutlet。如果您不想使用故事板,请将实施更改为:

var titleText : UILabel!

其次,应用程序崩溃是因为您要求它对尚未添加到层次结构的视图执行操作。为了便于阅读,我已经注释掉除了那些重要的行以外的所有行。见下文:

func createView() {
    //titleText = UILabel(frame: CGRect.zero)
    titleText.translatesAutoresizingMaskIntoConstraints = false // <-- You are trying to perform an operation on titleText here

    //titleText.text = "Tappy tap tap a square!"
    //titleText.textAlignment = .center
    //titleText.numberOfLines = 0

    self.addSubview(titleText) // <-- but you are adding it to the view hierarchy here

    //titleText.snp.makeConstraints { make in
        //make.edges.equalTo( self.snp.edges )
    //}
}
必须先将{p> titleText添加到视图层次结构中,然后才能在其上调用translatesAutoresizingMaskIntoConstraints,如下所示:

self.view.addSubview(titleText)
titleText.translatesAutoresizingMaskIntoConstraints = false // <-- Here we are waiting until the view has been added to the hierarchy to perform operations on it

因此,尝试将实现更改为:

import UIKit

class TitleView: UIView {

    var titleText: UILabel!

    override init(frame: CGRect) {
        // leave as is, removed for space
    }

    required init?(coder aDecoder: NSCoder) {
        // leave as is, removed for space
    }

    func createView() {
        titleText = UILabel(frame: CGRect.zero)
        titleText.text = "Tappy tap tap a square!"
        titleText.textAlignment = .center
        titleText.numberOfLines = 0

        self.addSubview(titleText)
        titleText.translatesAutoresizingMaskIntoConstraints = false

        titleText.snp.makeConstraints { make in
            make.edges.equalTo( self.snp.edges )
        }
    }
}