Home > Back-end >  NSLayoutconstraints not working as expected
NSLayoutconstraints not working as expected

Time:09-16

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)
    }
    
    }```

CodePudding user response:

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

CodePudding user response:

Your constraints are wrong. You are anchoring leadingAnchor to the trailingAnchor of of view and trailingAnchor to the leadingAnchor of view. Also you need to set translatesAutoresizingMaskIntoConstraints to false

Here is the correction:

private func closureInfoLabelSetup(){
    self.view.addSubview(closureInfoLabel)
    closureInfoLabel.translatesAutoresizingMaskIntoConstraints = false
    let constraints = [
        closureInfoLabel.leadingAnchor.constraint(equalTo: self.view.leadingAnchor,constant: 0),
        closureInfoLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0),
        closureInfoLabel.topAnchor.constraint(equalTo: closeRequestTitleLabel.bottomAnchor,constant: 1),
        closureInfoLabel.heightAnchor.constraint(equalToConstant: 40)
    ]
    NSLayoutConstraint.activate(constraints)
    print(closureInfoLabel,closureInfoLabel.frame)
}

  • Related