简体   繁体   中英

Applying Constraints Programmatically to UIImageView Subview

  1. I have a uiview that resizes with autolayout constraints.
  2. I have a uiimageview that I insert as a subview and then store, in a database, the distances from the leading/trailing/top/bottom edges to the uiview superview. Another note is the original uiimageview can be transformed and translated so CGPoint , CGAffineTransform , and CGRect are saved as well.
  3. I fetch from the database these "constant" values for each edge and then programmatically activate the constraints to an instantiated uiimageview to replicate the original uiimageview inserted.

My reason to add constraints is for the uiimageview to adapt proportionately to a change in the uiview size.

My issue is there's something wacky with my code that I cannot resolve on why I can't reproduce the original uiimageview based on the data I had stored, fetched, and then reapplied.

I'm admittedly lost when it comes to using constraints programmatically and any help would be appreciated.

//Storing constraint constants of original uiimageview in original container uiview
let leadingConstraint = originalImageView.frame.minX
let trailingConstraint = oldContainer.frame.maxX - originalImageView.frame.maxX
let topConstraint = originalImageView.frame.minY
let bottomConstraint = oldContainer.frame.maxY - originalImageView.frame.maxY

//Fetching and applying constants to new uiimageview in new container uiview (Separate operation from the above! Above takes place first!)

newImageView.bounds = CGRect(x: 0, y: 0, width: 80, height: 80)
newImageView.center = CGPoint(x: saved.x, y: saved.y)
newImageView.transform = CGAffineTransform(a: saved.a, b: saved.b, c: saved.c, d: saved.d, tx: saved.tx, ty: gifForm.ty)

newContainer.insertSubview(newImageView, at: 0)

newImageView.translatesAutoresizingMaskIntoConstraints = false
let leadingConstraint = newImageView.leadingAnchor.constraint(equalTo: newContainer.leadingAnchor, constant: saved.leadingConstant)
let trailingConstraint = newImageView.trailingAnchor.constraint(equalTo: newContainer.trailingAnchor, constant: -saved.trailingConstant)
let topConstraint = newImageView.topAnchor.constraint(equalTo: newContainer.topAnchor, constant: saved.topConstant)
let bottomConstraint = newImageView.bottomAnchor.constraint(equalTo: newContainer.bottomAnchor, constant: -saved.bottomConstant)
NSLayoutConstraint.activate([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])

Try exclusively using either constraints (setting translatesAutoresizingMaskIntoConstraints to false ) or not (leaving it true ).

You can't use both, meaning you can't use things like .center if you are planning to use constraints, but instead have to use the counterparts like centerXAnchor and centerYAnchor .

Also not sure if this is just negligence, but the four lines at the end use the same property names as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM