UIImageView框架不反映约束 [英] UIImageView Frame Doesn't Reflect Constraints

查看:172
本文介绍了UIImageView框架不反映约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIImageView ,并设置了一系列冲突约束。

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

我将其中一些的活动属性设置为 true 或到 false 在不同的时间,它的工作原理 - 图像视图总是在它应该是。

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.

但是,在另一种方法中,我使用它的框架来计算另一个视图的位置,所以我注意到它的框架不是图像视图出现的位置。例如,图像视图出现在屏幕中间的中心,但它的框架(我创建了另一个UIView,并设置它的框架到图像视图的框架来测试这个),在左下方 - 图像视图曾经是之前改变哪些约束是活动的。

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?

这是代码( qrCode 是我想安排的视图, myContainer 是其superview):

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)

所有这些代码正确定位了图像视图( qrCode ), (如 testView 所示)位于错误的位置 - 这是qrCode在配置约束之前的位置。

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.

推荐答案

这是答案:

在您的viewdidload中执行此操作:

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

这里的UIView,你需要使用作为一个子类,按我们的对话:

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


b $ b

这是每边的4点,这将为你调整大小,并且iamgeview设置了一个图像,如我在视图中显示的负载调用上面

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

这篇关于UIImageView框架不反映约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆