UIImageView 框架不反映约束
UIImageView Frame Doesn't Reflect Constraints
我有一个 UIImageView
,上面设置了一系列相互冲突的约束。
我在不同的时间将其中一些的 active
属性 设置为 true
或 false
,它起作用了——图像视图总是在它所在的位置应该是。
然而,在另一种方法中,我使用它的框架来计算另一个视图的位置,所以我注意到它的框架不是图像视图出现的位置。例如,图像视图显示在屏幕中间,但它的框架(我创建了另一个 UIView 并将其框架设置为图像视图的框架以测试它)位于左下角 - 图像视图曾经是的地方在更改哪些约束处于活动状态之前。
有什么方法可以更新框架,使其反映图像视图的实际位置?或者,有没有办法获得图像视图的真实框架?
这是代码(qrCode
是我要安排的视图,myContainer
是它的父视图):
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 所在的位置。
答案如下:
在您的 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:
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)
}
}
那是每边 4 个点,这将为您调整大小,并且 iamgeview 设置了一个图像,就像我在上面的加载调用视图中显示的那样
我有一个 UIImageView
,上面设置了一系列相互冲突的约束。
我在不同的时间将其中一些的 active
属性 设置为 true
或 false
,它起作用了——图像视图总是在它所在的位置应该是。
然而,在另一种方法中,我使用它的框架来计算另一个视图的位置,所以我注意到它的框架不是图像视图出现的位置。例如,图像视图显示在屏幕中间,但它的框架(我创建了另一个 UIView 并将其框架设置为图像视图的框架以测试它)位于左下角 - 图像视图曾经是的地方在更改哪些约束处于活动状态之前。
有什么方法可以更新框架,使其反映图像视图的实际位置?或者,有没有办法获得图像视图的真实框架?
这是代码(qrCode
是我要安排的视图,myContainer
是它的父视图):
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 所在的位置。
答案如下:
在您的 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:
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)
}
}
那是每边 4 个点,这将为您调整大小,并且 iamgeview 设置了一个图像,就像我在上面的加载调用视图中显示的那样