Autolayout programmatically doesn't work

后端 未结 1 1508
闹比i
闹比i 2021-01-21 01:41

I want to add programmatically an UIImageView to a view and add constraints to center it, vertically and horizontally. With the storyboard it\'s works but not programmatically.<

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 02:20

    UPDATED TO SWIFT 3

    I tested your code and the problem i found was that you missed this line:

    transitionImageView.translatesAutoresizingMaskIntoConstraints = false

    Also if you want your image size is 60x68 you need to add a couple of constraints for the width and height like this:

    let constW = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 60)
    
    let constV = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 68)
    

    and add them to your UIImageView

    transitionImageView.addConstraint(constW)
    transitionImageView.addConstraint(constV)
    

    Otherwise the UIImageView will be the UIImage size even if you assign CGSize (because setTranslatesAutoresizingMaskIntoConstraints(false) disables the CGSize you assigned).

    So your viewDidLoad() method could be like this:

    override func viewDidLoad() {
        super.viewDidLoad()
        let transitionImageView = UIImageView()
        transitionImageView.contentMode = UIViewContentMode.scaleToFill
    
        //Don't forget this line
        transitionImageView.translatesAutoresizingMaskIntoConstraints = false
    
        let transitionImage = UIImage(named: "SoProxyLogo60pt.jpg")
        transitionImageView.image = transitionImage
        self.view.addSubview(transitionImageView)
    
        // Position
        let transitionImageViewConstraintCenterX = NSLayoutConstraint(item: transitionImageView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
        transitionImageViewConstraintCenterX.identifier = "Transition Image View Constraint Center X"
        let transitionImageViewConstraintCenterY = NSLayoutConstraint(item: transitionImageView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0)
        transitionImageViewConstraintCenterY.identifier = "Transition Image View Constraint Center Y"
    
        let constW = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 60)
    
        let constV = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 68)
    
        self.view.addConstraint(transitionImageViewConstraintCenterX)
        self.view.addConstraint(transitionImageViewConstraintCenterY)
        transitionImageView.addConstraint(constW) //self.view.addConstraint(constW) also works
        transitionImageView.addConstraint(constV) //self.view.addConstraint(constV) also works
    }
    

    0 讨论(0)
提交回复
热议问题