【问题标题】:Access UIView width at runtime在运行时访问 UIView 宽度
【发布时间】:2014-12-22 01:56:41
【问题描述】:

在 Swift 中,如何在运行时获取使用自动布局约束以编程方式创建的 UIView 的宽度(或高度)?

我试过view1.frame.size.width 和view1.frame.width,但都返回0。

我一直在关注MakeAppPie 的自动布局教程并添加了view1.layer.cornerRadius = view1.bounds.width/2 如下,但这没有效果,所以我在view1.bounds.width 上做了一个po 并得到了0:

class ViewController: UIViewController {

func makeLayout() {

    //Make a view
    let view1 = UIView()
    view1.setTranslatesAutoresizingMaskIntoConstraints(false)
    view1.backgroundColor = UIColor.redColor()


    //Make a second view
    let view2 = UIView()
    view2.setTranslatesAutoresizingMaskIntoConstraints(false)
    view2.backgroundColor = UIColor(red: 0.75, green: 0.75, blue: 0.1, alpha: 1.0)

    //Add the views
    view.addSubview(view1)
    view.addSubview(view2)


    //--------------- constraints

    //make dictionary for views
    let viewsDictionary = ["view1":view1,"view2":view2]

    //sizing constraints
    //view1
    let view1_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[view1(>=50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
    let view1_constraint_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[view1(50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

    view1.addConstraints(view1_constraint_H)
    view1.addConstraints(view1_constraint_V)

    //view2
    let view2_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:[view2(50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
    let view2_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:[view2(>=40)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

    view2.addConstraints(view2_constraint_H)
    view2.addConstraints(view2_constraint_V)

    //position constraints

    //views
    let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-100-[view2]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
    let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-136-[view1]-100-[view2]-100-|", options: NSLayoutFormatOptions.AlignAllTrailing, metrics: nil, views: viewsDictionary)

    view.addConstraints(view_constraint_H)
    view.addConstraints(view_constraint_V)

    view1.layer.cornerRadius = view1.bounds.width/2

    }

override func viewDidLoad() {
    super.viewDidLoad()

    func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.All.rawValue)

    }

    // Do any additional setup after loading the view, typically from a nib.

    view.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 1, alpha: 1.0)
    makeLayout()

    }
}

【问题讨论】:

  • view1.frame.width 应该可以工作。你能发布更多你的代码吗?
  • 感谢史蒂夫的回复 - 已添加上面的代码。
  • 你得到的是 0,因为此时自动布局机制尚未启动以调整视图大小。

标签: ios swift uiview


【解决方案1】:

添加这个,你就可以开始了:

override func viewDidLayoutSubviews() {
        println(self.view1.frame.size)
    }

您可以随意查看宽度或高度。

【讨论】:

  • 这也为我打印出 0,0。
  • @kakubei 我发现我的问题是在调用类初始化程序时我忘记实际传递帧,因此在类初始化中返回 0,0。 XCode 也没有给出任何错误。
【解决方案2】:

这些字段只有一件事:

self.view1.frame.size.width
self.view1.frame.size.height

如果视图已绘制到屏幕上,则它们只是实际的用户设备大小,否则它们就是您在情节提要中设置的大小。因此,如果您尝试在绘制视图之前执行某种计算,您将不得不以另一种方式计算宽度或高度!

【讨论】:

  • 还有什么办法?在 Xcode 7 及更低版本中,我没有任何问题。但是现在在 Xcode 8 中,我遇到了这个问题。我的 UIImageView 大小打印为 500,但我认为它的大小只有 50。
【解决方案3】:

你只能用这个

self.view1.frame.size.width
self.view1.frame.size.height

【讨论】:

    【解决方案4】:

    帧的宽度和高度返回零,因为在 viewDidLoad 中调用该函数时,它的大小尚未正确。你可以使用 viewDidAppear 来调用你需要的函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 2018-08-05
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      相关资源
      最近更新 更多