NSLayoutconstraints没有按照预期工作

2022-01-27 17:30:53 标签 iosswiftiphoneframenslayoutconstraint

下面的代码没有按照预期工作。InfoLabel未添加到视图中。我在viewDidLoad中提到的顺序中调用下面的方法。任何快速的帮助/见解都是很好的。

谢谢!

注意:我已经在视图控制器中声明了所有的变量,并且没有设置任何属性,直到各自的设置块被调用。

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

###translatesAutoresizingMaskIntoConstraints没有设置为false你的closureInfoLabel和closeRequestTitleLabel。

###你的约束是错误的。

你正在锚的引导锚的尾锚view and trailingAnchor to the leadingAnchor of viewview. Also you need to set. 你还需要设置

translatesAutoresizingMaskIntoConstraints为假

更正如下:

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)
}
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码