我正在Swift5中建立一个项目,我需要用户上传照片。我将其设置为用户可以打开ImagePicker并选择照片的地步,但是每当他们选择图像并返回到原始VC时,我都会收到SIGNAL SIGABRT错误(在帖子底部):
这是我以编程方式添加约束的地方:
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
}
我将图像放回imageView的位置:
func didSelect(image: UIImage?) {
self.imgView.image = image
self.global.uploadFile(imageView: self.uploadIcon.imageView!)
}
在其中添加子视图的位置:
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()
}
这是引发错误的地方:
2019-06-12 13:22:16.635903-0600 bZa [39792:1836482] ***由于未捕获的异常'NSGenericException'终止了应用程序,原因:'无法激活锚点约束,因为它们没有共同祖先。约束或其锚点是否引用了不同视图层次结构中的项目?那是非法的。'
答案 0 :(得分:0)
当前的问题是,您在没有评论祖先的视图之间添加了约束,因此请确认您添加了
view.addSubview(imgView)
view.addSubview(topLabel)
view.addSubview(inputBox)
view.addSubview(btn)
也不要忘记
imgView.translatesAutoresizingMaskIntoConstraints = false
topLabel.translatesAutoresizingMaskIntoConstraints = false
inputBox.translatesAutoresizingMaskIntoConstraints = false
btn.translatesAutoresizingMaskIntoConstraints = false