gpt4 book ai didi

ios - UIImageView 框架不反射(reflect)约束

转载 作者:行者123 更新时间:2023-11-29 01:42:36 32 4
gpt4 key购买 nike

我有一个 UIImageView,并在其上设置了一系列相互冲突的约束。

我在不同的时间将其中一些的 active 属性设置为 truefalse,它起作用了—— ImageView 是总是在它应该在的地方。

但是,在另一种方法中,我使用它的框架来计算另一个 View 的位置,因此我注意到它的框架不是 ImageView 出现的位置。例如, ImageView 显示在屏幕中间,但它的框架(我创建了另一个 UIView 并将其框架设置为 ImageView 的框架以测试它)位于左下角 - ImageView 曾经是的地方在更改哪些约束处于事件状态之前。

有没有一种方法可以更新框架,使其反射(reflect) ImageView 的实际位置?或者,有没有办法获得 ImageView 的真实框架?

这是代码(qrCode 是我试图安排的 View ,myContainer 是它的父 View ):

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)

所有这些代码都正确定位了 ImageView (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 设置为图像,就像我在上面的加载调用 View 中显示的那样

关于ios - UIImageView 框架不反射(reflect)约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32240629/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com