มีปัญหาในการเปิดใช้งานข้อจำกัดทางโปรแกรมและได้รับข้อผิดพลาด

เมื่อคืนฉันโพสต์คำถามที่นี่ ซึ่งฉันใช้สตอรี่บอร์ดเพื่อกำหนดข้อจำกัด ฉันตัดสินใจทดสอบด้วยจุดยึดและทำข้อจำกัดทางโปรแกรม

ฉันได้รับข้อผิดพลาดนี้:

การยกเลิกแอปเนื่องจากข้อยกเว้นที่ไม่ถูกตรวจจับ 'NSGenericException' เหตุผล: 'ไม่สามารถเปิดใช้งานข้อ จำกัด ด้วยจุดยึดได้และเนื่องจากไม่มีบรรพบุรุษร่วมกัน ข้อจำกัดหรือจุดยึดอ้างอิงรายการในลำดับชั้นมุมมองที่แตกต่างกันหรือไม่ นั่นผิดกฎหมาย

ฉันรู้ว่าสิ่งนี้เกี่ยวข้องกับการดูที่ไม่ได้เป็นมุมมองย่อยของมุมมองอื่น อย่างไรก็ตาม ฉันได้ตรวจสอบหลายครั้งแล้ว และฉันไม่รู้ว่าต้องทำอย่างไร

นี่คือรหัส:

import UIKit
import CoreData
class editViewController: UIViewController {

let screenSize: CGRect = UIScreen.main.bounds
let moContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

var editnotes: addednotes?




let mainbackgroundview: UIView =
{
    let view = UIView()
    view.backgroundColor = .black
    view.alpha = 0.3

    return view
}()


let backgroundview1: UIView =
{
    let view = UIView()
    view.backgroundColor = .purple

    return view
}()


let edittasktextview1: UITextView = {
    let tv = UITextView()
    tv.backgroundColor = .black
    return tv
}()

    let cancelButton : UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Cancel", for: .normal)
        button.addTarget(self, action: #selector(handleCancelButton), for: 
.touchUpInside)
    return button
}()

let doneButton1 : UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Done", for: .normal)
    button.addTarget(self, action: #selector(handleDoneButton), for: .touchUpInside)
    return button
}()



@objc func handleCancelButton() {

    edittasktextview1.endEditing(true)
    dismiss(animated: true, completion: nil)
}


@objc func handleDoneButton()
{
    dismiss(animated: true, completion: nil)

    if edittasktextview1.text.isEmpty == true
    {
        moContext.delete(editnotes!)
    }
    else
    {
        editnotes?.sNote = edittasktextview1.text
    }
    var error: NSError?
    do {
        // Save The object

        try moContext.save()
        print("SAVED")
    } catch let error1 as NSError {
        error = error1
    }

    edittasktextview1.endEditing(true)

}



override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationID"), object: nil)

}
override func viewDidAppear(_ animated: Bool) {
    //add something here
   // edittasktextview.becomeFirstResponder()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.tintColor = UIColor.white
    navigationItem.title = "Edit Tasks"
    edittasktextview1.text = editnotes?.sNote
    backgroundview1.backgroundColor = editnotes?.sPriorityColor
    edittasktextview1.backgroundColor = .clear
    setupviews()
    //adding gesture to the background view to dismiss keyboard
    let dismisskeyboardgesture = UITapGestureRecognizer()
    dismisskeyboardgesture.addTarget(self, action: #selector(dismisskeyboard))


    let maskPath = UIBezierPath.init(roundedRect: self.backgroundview1.bounds, byRoundingCorners:[.topLeft, .topRight], cornerRadii: CGSize.init(width: 20.0, height: 0))
    let maskLayer = CAShapeLayer()
    maskLayer.frame = self.backgroundview1.bounds
    maskLayer.path = maskPath.cgPath
    self.backgroundview1.layer.mask = maskLayer
     view.backgroundColor = .clear


    //view.addGestureRecognizer(dismisskeyboardgesture)
    view.addSubview(mainbackgroundview)
   mainbackgroundview.addSubview(backgroundview1)
// view.addSubview(backgroundview1)
    backgroundview1.addSubview(edittasktextview1)
    backgroundview1.addSubview(cancelButton)
    backgroundview1.addSubview(doneButton1)
}

    @objc func dismisskeyboard()
   {
    edittasktextview1.endEditing(true)
   }


func setupviews()
{
   mainbackgroundview.anchorToTop(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
    backgroundview1.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 45, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)

    edittasktextview1.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 40, leftConstant: 8, bottomConstant: 0, rightConstant: 8, widthConstant: 0, heightConstant: 150)

    cancelButton.anchor(edittasktextview1.bottomAnchor, left: nil, bottom: nil, right: view.rightAnchor, topConstant: 18, leftConstant: 0, bottomConstant: 0, rightConstant: 8, widthConstant: 40, heightConstant: 30)

   doneButton1.anchor(edittasktextview1.bottomAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 18, leftConstant: 8, bottomConstant: 0, rightConstant: 0, widthConstant: 40, heightConstant: 30)


}

}

ความช่วยเหลือจะได้รับการชื่นชมเนื่องจากฉันติดอยู่กับปัญหานี้มาเกือบ 18 ชั่วโมง :(

อัปเดต #1

ไม่ได้รับข้อผิดพลาดเดียวกันในขณะนี้ แต่ฉันไม่สามารถตั้งค่า backgroundview1 บน mainBackgroundview ได้

ฉันกำลังเพิ่มมันเป็นมุมมองย่อย:

mainbackgroundview.addSubview(backgroundview1)

ใน setupviews() ฉันกำลังวางข้อจำกัดดังนี้:

 NSLayoutConstraint.activate([
        backgroundview1.leftAnchor.constraint(equalTo: mainbackgroundview.leftAnchor, constant: 0),
        backgroundview1.rightAnchor.constraint(equalTo: mainbackgroundview.rightAnchor, constant: 0),
        backgroundview1.bottomAnchor.constraint(equalTo: mainbackgroundview.bottomAnchor, constant: 0),
        backgroundview1.topAnchor.constraint(equalTo: mainbackgroundview.topAnchor, constant: 45)

        ])

ถึงกระนั้น ฉันยังไม่ได้รับ backgroundview1 ที่จะวางไว้บน mainbackgroundview mainbackgroundview กำลังแสดงขึ้นมาบนเครื่องจำลอง

ฉันกำลังเพิ่ม mainbackgroundview เช่น:

 NSLayoutConstraint.activate([
        mainbackgroundview.leftAnchor.constraint(equalTo: view.leftAnchor),
        mainbackgroundview.rightAnchor.constraint(equalTo: view.rightAnchor),
        mainbackgroundview.topAnchor.constraint(equalTo: view.topAnchor),
        mainbackgroundview.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

ฉันทำอะไรผิดตอนนี้?


person Osama Naeem    schedule 05.12.2017    source แหล่งที่มา
comment
คุณยังพลาดที่จะตั้งค่า yourView.translatesAutoresizingMaskIntoConstraints=false ในมุมมองทั้งหมดของคุณ นอกจากนี้ให้ตั้งค่า .isActive=true สำหรับข้อ จำกัด ด้วย   -  person Tushar Sharma    schedule 05.12.2017
comment
พยายามอย่างนั้น ยังไม่ทำงาน :(   -  person Osama Naeem    schedule 05.12.2017
comment
mainwallpaperview.addSubview (พื้นหลังวิว1) สิ่งนี้จะเพิ่ม backgroundView1 บน mainBackviewview ดังนั้นข้อจำกัดสำหรับ backgroundView1 ควรอ้างอิงถึง mainBackgroundview ไม่ใช่ view   -  person Tushar Sharma    schedule 05.12.2017


คำตอบ (1)


ในฟังก์ชัน setupViews() คุณกำลังเพิ่มข้อจำกัดแต่ไม่ได้ตั้งค่าให้ใช้งานอยู่ นี่คือข้อจำกัดจุดยึดตัวอย่างที่สามารถเพิ่มได้สำเร็จ:

myView.bottomAnchor.constraint(equalTo: view.topAnchor,
   constant: 8).isActive=true 

ลองเพิ่ม .isActive ต่อท้ายข้อจำกัดแต่ละข้อของคุณ

ยิ่งไปกว่านั้น คุณสามารถเพิ่มข้อจำกัดของจุดยึดที่เกี่ยวข้องได้ เช่น topAnchor, BottomAnchor, widthAnchor, heightAnchor, LeadingAnchor เป็นต้น ลองทำให้ข้อจำกัดของคุณง่ายขึ้น

person Amrit Sidhu    schedule 05.12.2017
comment
ฉันได้อัปเดตโพสต์แล้ว ฉันไม่ได้รับข้อผิดพลาดเดียวกัน แต่ดูเหมือนจะไม่สามารถกำหนดข้อจำกัดสำหรับมุมมองที่ฉันวางไว้ด้านบนของมุมมองพื้นหลังหลักได้ โปรดตรวจสอบ! - person Osama Naeem; 05.12.2017
comment
เฮ้พี่ชาย ฉันเพิ่งตรวจสอบรหัสของคุณ คุณกำลังนำไปใช้ในลำดับที่ไม่ถูกต้อง สามารถเพิ่มข้อจำกัดได้เมื่อคุณเพิ่มมุมมองลงในมุมมองหลักแล้วเท่านั้น เรียก setupviews() ที่ส่วนท้ายของ viewdidload() - person Amrit Sidhu; 05.12.2017