Search code examples
iosswiftiphoneframenslayoutconstraint

NSLayoutconstraints not working as expected


Below code is not working as expected. the InfoLabel is not added to the view. I am calling the below methods in the order mentioned from viewDidLoad. Any quick help/insights will be great. Thanks! Note : I have declared all variables in the viewcontroller and not setting any properties until the respective setup blocks are called.

       
        self.view.addSubview(cancelButton)
        cancelButton.translatesAutoresizingMaskIntoConstraints = false
        cancelButton.addTarget(self, action: #selector(cancelButtonClicked(_:)), for: .touchUpInside)
        let constraints = [
            cancelButton.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
            cancelButton.widthAnchor.constraint(equalToConstant: 40),
            cancelButton.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
            cancelButton.heightAnchor.constraint(equalToConstant: 40)
        ]
        NSLayoutConstraint.activate(constraints)
        print(cancelButton.frame)
    }
    
    private func saveButtonSetup(){
        
        self.view.addSubview(saveButton)
        saveButton.translatesAutoresizingMaskIntoConstraints = false
        saveButton.addTarget(self, action: #selector(saveButtonClicked(_:)), for: .touchUpInside)
        let constraints = [
            saveButton.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
            saveButton.widthAnchor.constraint(equalToConstant: 40),
            saveButton.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
            saveButton.heightAnchor.constraint(equalToConstant: 40)
        ]
        NSLayoutConstraint.activate(constraints)
        print(saveButton.frame)
    }
    
    private func TitleLabelSetup(){
       TitleLabel.backgroundColor = UIManager.Colors.cellBGColor
        TitleLabel.textAlignment = .center
        TitleLabel.font = UIManager.Fonts.titleFont
       TitleLabel.textColor = UIManager.Colors.titleColor
        self.view.addSubview(TitleLabel)
        let constraints = [
            TitleLabel.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor/*self.cancelButton.trailingAnchor*/,constant: 0),
           TitleLabel.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor/*self.saveButton.leadingAnchor*/, constant: 0),
            TitleLabel.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
            TitleLabel.heightAnchor.constraint(equalToConstant: 40)
        ]
        NSLayoutConstraint.activate(constraints)
        print(TitleLabel.frame)
    }
    
    private funcInfoLabelSetup(){
        self.view.addSubview(InfoLabel)
        let constraints = [
            InfoLabel.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor,constant: 0),
           InfoLabel.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
            InfoLabel.topAnchor.constraint(equalTo: TitleLabel.bottomAnchor,constant: 1),
            Label.heightAnchor.constraint(equalToConstant: 40)
        ]
        NSLayoutConstraint.activate(constraints)
        print(InfoLabel,InfoLabel.frame)
    }
    
    }```

Solution

  • translatesAutoresizingMaskIntoConstraints is not set to false for your closureInfoLabel and closeRequestTitleLabel.