[英]Swift ImagePicker Throws SIGNAL SIGABRT on AutoLayout Constraints
我正在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'而終止應用程序,原因:'無法使用錨點激活約束,因為它們沒有共同的祖先。 約束或其錨點是否引用不同視圖層次結構中的項目? 這是非法的。
目前的問題是您在沒有評論祖先的視圖之間添加約束,因此請驗證您是否添加
view.addSubview(imgView)
view.addSubview(topLabel)
view.addSubview(inputBox)
view.addSubview(btn)
也別忘了
imgView.translatesAutoresizingMaskIntoConstraints = false
topLabel.translatesAutoresizingMaskIntoConstraints = false
inputBox.translatesAutoresizingMaskIntoConstraints = false
btn.translatesAutoresizingMaskIntoConstraints = false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.