2016-05-30 44 views
1

時,遇到了SIGABRT我有下面的代碼:使用SnapKit庫

// ViewController.swift 
// Copypasta Keyboard 
// 
// Created by vroy on 5/30/16. 
// Copyright © 2016 vroy. All rights reserved. 
// 

import UIKit 
import SnapKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     let thankYouMessage = UILabel() 
     thankYouMessage.text = "Thank You for Installing the Keyboard." 
     thankYouMessage.textAlignment = .Center 
     thankYouMessage.numberOfLines = 1 


     thankYouMessage.snp_makeConstraints { (make) -> Void in 
       //The program crashes if either of the following two lines are uncommented. 
       make.top.left.right.equalTo(0) 
       make.height.equalTo(self.view.snp_height).multipliedBy(0.2) 
     } 


     self.view.addSubview(thankYouMessage) 


    } 

} 

如果其中的兩行:

make.top.left.right.equalTo(0) 

make.height.equalTo(self.view.snp_height).multipliedBy(0.2) 

執行我得到一個SIGABRT錯誤:

我正在使用SnapKit庫版本0.19.0

我該怎麼辦?

回答

0

您需要在製作約束之前添加子視圖。所以把代碼變成這樣:

let thankYouMessage = UILabel() 
    thankYouMessage.text = "Thank You for Installing the Keyboard." 
    thankYouMessage.textAlignment = .Center 
    thankYouMessage.numberOfLines = 1 

    self.view.addSubview(thankYouMessage) 

thankYouMessage.snp_makeConstraints { (make) -> Void in 
       //The program crashes if either of the following two lines are uncommented. 
       make.top.left.right.equalTo(0) 
       make.height.equalTo(self.view.snp_height).multipliedBy(0.2) 
     }