2014-10-26 51 views
15

在Swift中,如何獲得在運行時使用自動佈局約束條件創建的UIView的寬度(或高度)?在運行時訪問UIView寬度

我試過view1.frame.size.widthview1.frame.width,但都返回0

我一直在下面通過MakeAppPie自動佈局教程,並添加view1.layer.cornerRadius = view1.bounds.width/2如下,但是這並沒有影響,所以我做了保安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() 

    } 
} 
+1

view1.frame.width應該可以工作。你能發佈更多的代碼嗎? – 2014-10-26 21:52:52

+1

感謝您的回覆史蒂夫 - 添加了上面的代碼。 – gemello 2014-10-26 22:15:03

+0

您在那裏獲得0,因爲此時自動佈局機制尚未開始調整視圖大小。 – JDS 2014-10-26 22:28:55

回答

45

添加這一點,你是很好去:

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

你可以看看你的寬度或高度。

+0

完美 - 謝謝史蒂夫! – gemello 2014-10-27 20:43:52

+2

這也爲我打印出0,0。 – kakubei 2015-07-10 08:37:32

+0

@kakubei我發現我的問題是當調用類初始化程序時,我忘了實際傳遞幀,因此在類初始化中返回0,0。 XCode也沒有給出任何錯誤。 – 2015-09-01 16:46:57

3

,你只能使用此

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

一兩件事有關這些字段:

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

是如果視圖都被吸引到屏幕只有實際的用戶設備的大小,否則他們是什麼都你將它們設置在故事板中。因此,如果您在繪製視圖之前嘗試執行某種計算,則必須以另一種方式計算寬度或高度!

+0

和什麼是方式?在Xcode 7及以下版本中,我沒有任何問題。但是現在在Xcode 8中,我遇到了這個問題。我的UIImageView尺寸打印500,但我認爲它只有50尺寸。 – jo3birdtalk 2016-09-25 08:55:30