HomeiOS Developmentios - Unable to activate constraint with anchors.... as a result of...

ios – Unable to activate constraint with anchors…. as a result of they haven’t any frequent ancestor


I’m trying to create a Swift-based UI programmatically that resembles the format proven under

enter image description here

Beneath is my view hierarchy which I created utilizing xib. Now I am trying to attain the identical via programatically.

here

Beneath is the code which i am trying. Right here JTACDayCell is the kid of UICollectionViewCell

FYI: JTACDayCell belongs to JTAppleCalendar library

class DateTableCell: JTACDayCell {
static let reuseID = "dateCell"
lazy var parentView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

lazy var selectedView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

lazy var dateLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

lazy var eventView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

lazy var separatorView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .lightGray
    return view
}()

override init(body: CGRect) {
    tremendous.init(body: body)
    setUpAutoLayout()
    
    parentView.addSubview(selectedView)
    parentView.addSubview(dateLabel)
    parentView.addSubview(eventView)
    
    addSubview(parentView)
    addSubview(separatorView)
}

required init?(coder: NSCoder) {
    fatalError("init?(coder: NSCoder) has not been carried out")
}

non-public func setUpAutoLayout() {
    NSLayoutConstraint.activate([
        parentView.leftAnchor.constraint(equalTo: leftAnchor),
        parentView.rightAnchor.constraint(equalTo: rightAnchor),
        parentView.topAnchor.constraint(equalTo: topAnchor),
        parentView.bottomAnchor.constraint(equalTo: separatorView.topAnchor),
        
        selectedView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor),
        selectedView.centerYAnchor.constraint(equalTo: parentView.centerYAnchor, constant: -2.5),
        selectedView.widthAnchor.constraint(equalToConstant: 35),
        selectedView.heightAnchor.constraint(equalToConstant: 35),
        
        dateLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor),
        dateLabel.centerYAnchor.constraint(equalTo: parentView.centerYAnchor, constant: -2.5),
        
        eventView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor),
        eventView.widthAnchor.constraint(equalToConstant: 6),
        eventView.heightAnchor.constraint(equalToConstant: 6),
        eventView.bottomAnchor.constraint(equalTo: parentView.topAnchor, constant: 5),
        
        separatorView.leftAnchor.constraint(equalTo: leftAnchor),
        separatorView.rightAnchor.constraint(equalTo: rightAnchor),
        separatorView.heightAnchor.constraint(equalToConstant: 0.5),
        separatorView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 1)
    ])
}
}

Upon executing the supplied code, I encounter the next error

*** Terminating app because of uncaught exception ‘NSGenericException’, cause: ‘Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x6000034619c0 “UIView:0x7fb1a8a20ce0.left”> and <NSLayoutXAxisAnchor:0x600003460800 “AppName.DateTableCell:0x7fb1a8a28470.left”> as a result of they haven’t any frequent ancestor. Does the constraint or its anchors reference gadgets in several view hierarchies? That is unlawful.’

What could be the difficulty right here?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments