Search code examples
iossnapkit

Getting SIGABRT when using SnapKit library


I have the following piece of code:

//  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)


    }

}

If either of the two lines:

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

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

are executed I get a SIGABRT error:

enter image description here

I am using the SnapKit library version 0.19.0

What should I do?


Solution

  • You need to add the subview before making the constraints. So turn the code into this:

        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)
            }