简体   繁体   中英

Swift ImagePicker Throws SIGNAL SIGABRT on AutoLayout Constraints

I'm building a project in Swift5 and I need the user to upload a photo. I have it to the point where the user can open the ImagePicker and select a photo, but whenever they select the image and return to the original VC, I get a SIGNAL SIGABRT error (at bottom of post):

Here is where I add my constraints programatically:

func setupLayout(){
    imgView.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
    imgView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    imgView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    imgView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    imgView.heightAnchor.constraint(equalToConstant: 125).isActive = true

    topLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    topLabel.topAnchor.constraint(equalTo: imgView.bottomAnchor, constant: 60).isActive = true
    topLabel.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -50).isActive = true
    topLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
    topLabel.adjustsFontSizeToFitWidth = true

    inputBox.topAnchor.constraint(equalTo: topLabel.bottomAnchor, constant: 30).isActive = true
    inputBox.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    inputBox.heightAnchor.constraint(equalToConstant: 50).isActive = true
    inputBox.widthAnchor.constraint(equalToConstant: 250).isActive = true

    btn.topAnchor.constraint(equalTo: inputBox.bottomAnchor, constant: 40).isActive = true
    btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    let navBarImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    navBarImageView.contentMode = .scaleAspectFit
    let navBarImage = UIImage(named: "bzaLogo")
    navBarImageView.image = navBarImage
    self.navigationController?.navigationItem.titleView = navBarImageView
}

And where I set the image back on the imageView:

func didSelect(image: UIImage?) {
    self.imgView.image = image
    self.global.uploadFile(imageView: self.uploadIcon.imageView!)
}

And where I add the subviews:

override func viewDidLoad() {
    super.viewDidLoad()
    currentState = 0
    imgView.translatesAutoresizingMaskIntoConstraints = false
    topLabel.translatesAutoresizingMaskIntoConstraints = false
    inputBox.translatesAutoresizingMaskIntoConstraints = false
    btn.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(imgView)
    view.addSubview(topLabel)
    view.addSubview(inputBox)
    view.addSubview(btn)

    inputBox.addTarget(self, action: #selector(inputBoxClicked(textField:)), for: .touchDown)
    imagePicker = ImagePicker(presentationController: self, delegate: self)
    viewModel.state = currentState
    inputBox.delegate = self

    setupLayout()
}

And here is the error getting thrown:

2019-06-12 13:22:16.635903-0600 bZa[39792:1836482] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'

Currently the problem is that you add constraints between views who has no comment ancestor so verify that you add

view.addSubview(imgView)  
view.addSubview(topLabel) 
view.addSubview(inputBox)
view.addSubview(btn)

Also don't forget

imgView.translatesAutoresizingMaskIntoConstraints = false 
topLabel.translatesAutoresizingMaskIntoConstraints = false
inputBox.translatesAutoresizingMaskIntoConstraints = false
btn.translatesAutoresizingMaskIntoConstraints = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM