1
我試圖做所有事情都沒有故事板,但添加我的標籤時仍然沒有零,但我不明白爲什麼。我確信它的死亡簡單,即時通訊是盲目的,但我只是不能看到它後很多谷歌搜索。Stackview標籤沒有以編程方式添加
視圖控制器:
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
有:
fatal error: unexpectedly found nil while unwrapping an Optional value
任何幫助將是偉大的,即時通訊確定其簡單,但我不能再看到了。謝謝
非常感謝。我嘗試重新排序,但iboutlet是主要問題。 – Wazza
沒問題! @WayneRumble – Wes