ios 在运行时访问 UIView 宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26578234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:22:25  来源:igfitidea点击:

Access UIView width at runtime

iosswiftuiview

提问by gemello

In Swift, how do I get the width (or height) at runtime, of a UIView that's created programmatically using auto layout constraints?

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

I've tried view1.frame.size.widthand view1.frame.width, but both return 0.

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

I've been following an auto layout tutorial by MakeAppPieand added view1.layer.cornerRadius = view1.bounds.width/2as below, but this had no effect, so I did a po on view1.bounds.widthand got 0:

我一直在关注MakeAppPie的自动布局教程并添加view1.layer.cornerRadius = view1.bounds.width/2如下,但这没有效果,所以我做了一个 po view1.bounds.width并得到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()

    }
}

回答by Steve Rosenberg

Add this and you are good to go:

添加这个,你很高兴:

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

you can look at width or height as you please.

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

回答by Craig

Just one thing about these fields:

关于这些领域只有一件事:

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

Are only actual user device size if the view has been drawn to screen, else they are what ever you set them as in the storyboard. So if your trying to perform some kind of calculation before the view is drawn you will have to figure out the width or height another way!

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

回答by Shahryar

you can only use this

你只能使用这个

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