【问题标题】:UIImageView Frame Doesn't Reflect ConstraintsUIImageView 框架不反映约束
【发布时间】:2015-08-27 04:17:12
【问题描述】:

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

我在不同的时间将其中一些的active 属性设置为truefalse,它可以工作——图像视图总是在它应该在的位置。

但是,在另一种方法中,我使用它的框架来计算另一个视图的位置,所以我注意到它的框架不是图像视图出现的位置。例如,图像视图出现在屏幕中间,但它的框架(我创建了另一个 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 所在的位置。

【问题讨论】:

  • 您使用的是故事板还是原始代码?
  • @Larcerax Both — 原始图像视图和约束是在情节提要中制作的,但我为用于更改 active 属性的约束创建了出口。
  • 您是否使用约束常量或其他方式为约束设置动画,或者如果您根本不为它们设置动画,您是否至少使用常量来更改 UIImageView 的位置?常量如下: NSLayoutConstraint(item: labelMessage, attribute: .Top, relatedBy: .Equal, toItem: contentView, attribute: .Top, multiplier: 1.0, constant: 35) ... 常量是 35
  • 我根本没有为约束设置动画,也没有更改常量。我唯一要做的就是将 some 的 active 属性设置为 true,将其他的 active 属性设置为 false。
  • 啊,你在使用“setTranslatesAutoResizginMaskIntoConstraints = FALSE”

标签: ios swift uiview autolayout constraints


【解决方案1】:

答案如下:

在您的 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 设置了一个图像,就像我在上面的视图中显示的那样加载调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多