[英]Swift ImagePicker Throws SIGNAL SIGABRT on AutoLayout Constraints
I'm building a project in Swift5 and I need the user to upload a photo. 我正在Swift5中构建一个项目,我需要用户上传照片。 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):
我已经到了用户可以打开ImagePicker并选择照片的程度,但每当他们选择图像并返回到原始VC时,我都会收到SIGNAL SIGABRT错误(在帖子底部):
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: 我将图像设置在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. 2019-06-12 13:22:16.635903-0600 bZa [39792:1836482] ***由于未捕获的异常'NSGenericException'而终止应用程序,原因:'无法使用锚点激活约束,因为它们没有共同的祖先。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.