annaFerdsf
annaFerdsf

Reputation: 339

How to add constrains inside custom UIView?

I want to add constrainst to UILabel My custom UIView class:

class LabelView: UIView {

    public var label: UILabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = frame
        prepareUI()
    }  
    func prepareUI() {     
        self.label.text = "SOME TEXT"
        self.label.sizeToFit()
        self.label.adjustsFontSizeToFitWidth = true
        self.label.textAlignment = .center
        self.addSubview(self.label)
        
        self.translatesAutoresizingMaskIntoConstraints = false
        self.label.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.label.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true
        self.label.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.2).isActive = true
    }
}

after running I get the following error and the UILabel is placed in the top left corner of the UIView:

2021-02-13 02:32:26.698694+1000 SwiftHello[96793:1625524] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x600000606c10 h=--& v=--& UILabel:0x142616930.minX == 0 (active, names: '|':SwiftHello.LabelView:0x1426167c0 )>", "<NSAutoresizingMaskLayoutConstraint:0x600000606c60 h=--& v=--& UILabel:0x142616930.width == 93.5 (active)>", "<NSLayoutConstraint:0x6000006061c0 UILabel:0x142616930.centerX == SwiftHello.LabelView:0x1426167c0.centerX (active)>", "<NSLayoutConstraint:0x600000606300 UILabel:0x142616930.width == 0.5*SwiftHello.LabelView:0x1426167c0.width (active)>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6000006061c0 UILabel:0x142616930.centerX == SwiftHello.LabelView:0x1426167c0.centerX (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-02-13 02:32:26.698920+1000 SwiftHello[96793:1625524] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x600000606cb0 h=--& v=--& UILabel:0x142616930.minY == 0 (active, names: '|':SwiftHello.LabelView:0x1426167c0 )>", "<NSAutoresizingMaskLayoutConstraint:0x600000606d00 h=--& v=--& UILabel:0x142616930.height == 20.5 (active)>", "<NSLayoutConstraint:0x6000006062b0 UILabel:0x142616930.centerY == SwiftHello.LabelView:0x1426167c0.centerY (active)>", "<NSLayoutConstraint:0x600000606350 UILabel:0x142616930.height == 0.2*SwiftHello.LabelView:0x1426167c0.height (active)>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6000006062b0 UILabel:0x142616930.centerY == SwiftHello.LabelView:0x1426167c0.centerY (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-02-13 02:32:26.702639+1000 SwiftHello[96793:1625524] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x600000606c60 h=--& v=--& UILabel:0x142616930.width == 93.5 (active)>", "<NSLayoutConstraint:0x600000606300 UILabel:0x142616930.width == 0.5SwiftHello.LabelView:0x1426167c0.width (active)>", "<NSLayoutConstraint:0x600000606940 SwiftHello.LabelView:0x1426167c0.width == 0.85UIView:0x14260e930.width (active)>", "<NSLayoutConstraint:0x600000606da0 'UIView-Encapsulated-Layout-Width' UIView:0x14260e930.width == 375 (active)>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600000606300 UILabel:0x142616930.width == 0.5*SwiftHello.LabelView:0x1426167c0.width (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-02-13 02:32:26.702816+1000 SwiftHello[96793:1625524] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x600000606d00 h=--& v=--& UILabel:0x142616930.height == 20.5 (active)>", "<NSLayoutConstraint:0x600000606350 UILabel:0x142616930.height == 0.2SwiftHello.LabelView:0x1426167c0.height (active)>", "<NSLayoutConstraint:0x600000606990 SwiftHello.LabelView:0x1426167c0.height == 0.1UIView:0x14260e930.height (active)>", "<NSLayoutConstraint:0x600000606d50 'UIView-Encapsulated-Layout-Height' UIView:0x14260e930.height == 667 (active)>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600000606350 UILabel:0x142616930.height == 0.2*SwiftHello.LabelView:0x1426167c0.height (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

Upvotes: 0

Views: 139

Answers (1)

DonMag
DonMag

Reputation: 77672

You're missing this line:

self.label.translatesAutoresizingMaskIntoConstraints = false

You've set it on self but not on the label.

Upvotes: 1

Related Questions