UIImageView Frame Doesn't Reflect Constraints

565 Views Asked by At

I have a UIImageView, and a series of conflicting constraints set on it.

I set the active property of some of them to true or to false at different times, and it works—the image view is always where it's supposed to be.

However, in another method, I use it's frame to calculate the position of another view, and so I noticed that it's frame isn't where the image view appears. For example, the image view appears centered in the middle of the screen, but it's frame (I created another UIView and set it's frame to the image view's frame to test this), is in the bottom left—where the image view used to be before changing which constraints were active.

Is there a way I can update the frame so that it reflects where the image view actually is? Or, is there a way to get the true frame of the image view?

Here's the code (qrCode is the view I'm trying to arrange, myContainer is its superview):

self.qrCode.setTranslatesAutoresizingMaskIntoConstraints(false)

//this sets qrCode 0 pts from the bottom of its parent
self.bottom.active = false

//this centers qrCode vertically in its parent
self.center.active = true 

//these set how far the parent is from the edges of its view
self.newLeft.active = true
self.newRight.active = true

let testView = UIView(frame: qrCode.frame)
testView.backgroundColor = UIColor.greenColor()
self.myContainer.addSubview(testView)

All this code positions the image view (qrCode) correctly, but it's frame (as shown by testView is in the wrong location—it's where qrCode was before the constraints were configured.

1

There are 1 best solutions below

0
On BEST ANSWER

Here's the answer:

Do this in your viewdidload:

@IBOutlet weak var asdf = QQCustomUIViewForQRImageView()

override func viewDidLoad() {
super.viewDidLoad()

    var floatiesW = 200 as CGFloat
    var floatiesH = 200 as CGFloat
    asdf!.frame = CGRectMake((self.view.bounds.width/2.0) - (floatiesW/2.0), (self.view.bounds.height/2.0) - (floatiesH/2.0), floatiesW, floatiesH)
    asdf!.qrImageView.image = UIImage(named: "check")
}

Here's the UIView you'll need to use as a subclass, as per our conversation:

import UIKit

class QQCustomUIViewForQRImageView: UIView  {

    var qrImageView = UIImageView()

    override init (frame : CGRect) {
        super.init(frame : frame)
    }

    convenience init () {
        self.init(frame:CGRect.zeroRect)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.backgroundColor = UIColor.redColor()

        qrImageView.setTranslatesAutoresizingMaskIntoConstraints(false)
        qrImageView.backgroundColor = UIColor.yellowColor()
        self.addSubview(qrImageView)

        let views: [NSObject : AnyObject] = [ "qrImageView" : qrImageView]
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-4-[qrImageView]-4-|", options: nil, metrics: nil, views: views))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-4-[qrImageView]-4-|", options: nil, metrics: nil, views: views))
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

that's 4 points on each side, this will resize for you, and the iamgeview is set with an image like I show in the view did load call above