AutoLayout不管是在StoryBorad還是在xib中都相對來說比較簡單,VFL(Visual fromat language)可視化語言基本上用到的比較少,在xCode4時候自動布局的概念還沒有,直接使用VFL會很友善,可視化語言依賴于oc運作時建立對應的限制,如果IBOutlet發生改變有的時候會造成莫名其妙的Bug。xCode5之後可視化語言用到的場景相對較少,但是作為一個工作的輔助還是可以稍微了解下。
在StotyBoard中添加一個标簽一個按鈕,不适用自動布局,簡單的控制它們之間的水準距離為80,如下圖所示:

視圖中添加限制:
1
2
<code>NSLayoutConstraint</code> <code>*labelContraint=[</code><code>NSLayoutConstraint</code> <code>constraintWithItem:</code><code>self</code><code>.changeButton attribute:</code><code>NSLayoutAttributeLeft</code> <code>relatedBy:</code><code>NSLayoutRelationEqual</code> <code>toItem:</code><code>self</code><code>.descriptionLabel attribute:</code><code>NSLayoutAttributeRight</code> <code>multiplier:1.0 constant:60];</code>
<code>[</code><code>self</code><code>.view addConstraint:labelContraint];</code>
這個隻是視圖限制的一種方式,下面這種方式才是本文的主角:
3
4
<code>//使用可視化語言添加限制</code>
<code>NSDictionary</code> <code>*viewDictionary=</code><code>NSDictionaryOfVariableBindings</code><code>(_descriptionLabel,_changeButton);</code>
<code>NSArray</code> <code>*visualConstraint=[</code><code>NSLayoutConstraint</code> <code>constraintsWithVisualFormat:@</code><code>"[_descriptionLabel]-60-[_changeButton]"</code> <code>options:0 metrics:</code><code>nil</code> <code>views:viewDictionary];</code>
<code>[</code><code>self</code><code>.view addConstraints:visualConstraint];</code>
這裡面用到的constraintsWithVisualFormat方法,具體參數說明如下:
format:參數是vfl語句,語句的基本元素下面會詳細解釋一下;
opts:枚舉參數,預設寫0;
metrics:字典,當在format中使用了動态資料,會根據字典去比對,接下來會具體有例子;
views:字典,傳入需要用到的視圖集合;
具體format需要參考一下表達式的意思:
水準方向 H:
垂直方向 V:
Views [需要定義的視圖]
SuperView |
關系 >=,==,<=
間隙 -
視圖内部限制 ()
通過VFL控制手動添加的标簽的位置,具體效果如下:
代碼實作如下:
5
6
7
8
9
10
11
<code>UILabel *link=[[UILabel alloc]init];</code>
<code>link.text=@</code><code>"http://www.cnblogs.com/xiaofeixiang"</code><code>;</code>
<code>link.translatesAutoresizingMaskIntoConstraints=</code><code>NO</code><code>;</code>
<code>[link setBackgroundColor:[UIColor greenColor]];</code>
<code>[</code><code>self</code><code>.view addSubview:link];</code>
<code>NSArray</code> <code>*horizontal=[</code><code>NSLayoutConstraint</code> <code>constraintsWithVisualFormat:@</code><code>"H:|-40-[link]-20-|"</code> <code>options:0 metrics:</code><code>nil</code> <code>views:</code><code>NSDictionaryOfVariableBindings</code><code>(link)];</code>
<code>NSArray</code> <code>*vertical=[</code><code>NSLayoutConstraint</code> <code>constraintsWithVisualFormat:@</code><code>"V:[_descriptionLabel]-100-[link(>=30)]"</code> <code>options:0 metrics:</code><code>nil</code> <code>views:</code><code>NSDictionaryOfVariableBindings</code><code>(link,_descriptionLabel)];</code>
<code>[</code><code>self</code><code>.view addConstraints:horizontal];</code>
<code>[</code><code>self</code><code>.view addConstraints:vertical];</code>
第一個限制是控制标簽距離父視圖左右之間的距離,第二個控制标簽和”部落格園-FlyElephant"之間的垂直距離為100.當然如果你想通過字典控制垂直之間的距離可以按照下面這麼做:
<code>NSArray</code> <code>*vertical=[</code><code>NSLayoutConstraint</code> <code>constraintsWithVisualFormat:@</code><code>"V:[_descriptionLabel]-Vertical-[link(>=30)]"</code> <code>options:0 metrics:@{@</code><code>"Vertical"</code><code>:</code><code>@200</code><code>} views:</code><code>NSDictionaryOfVariableBindings</code><code>(link,_descriptionLabel)];</code>
最後的結果:
友情提示在添加限制的時候不要和StoryBoard中的沖突,如果添加的水準限制StoryBoard中也有的話,就會出現下面這種情況:
12
<code>2015-07-01 10:54:13.537 VFLDemo[2358:60863] Unable to simultaneously satisfy constraints.</code>
<code> </code><code>Probably at least one of the constraints in the following list is one you don</code><code>'t want. Try this: (1) look at each constraint and try to figure out which you don'</code><code>t expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you</code><code>'re seeing NSAutoresizingMaskLayoutConstraints that you don'</code><code>t understand, refer to the documentation </code><code>for</code> <code>the UIView property translatesAutoresizingMaskIntoConstraints)</code>
<code>(</code>
<code> </code><code>"<NSLayoutConstraint:0x7fc5e3732860 H:[UILabel:0x7fc5e372ef30'\U535a\U5ba2\U56ed-FlyElephant']-(15)-[UIButton:0x7fc5e372d550'\U7fa4:228407086']>"</code><code>,</code>
<code> </code><code>"<NSLayoutConstraint:0x7fc5e37344e0 H:[UILabel:0x7fc5e372ef30'\U535a\U5ba2\U56ed-FlyElephant']-(60)-[UIButton:0x7fc5e372d550'\U7fa4:228407086']>"</code>
<code>)</code>
<code>Will attempt to recover by breaking constraint</code>
<code><</code><code>NSLayoutConstraint</code><code>:0x7fc5e37344e0 H:[UILabel:0x7fc5e372ef30</code><code>'部落格園-FlyElephant'</code><code>]-(60)-[UIButton:0x7fc5e372d550</code><code>'群:228407086'</code><code>]></code>
<code>Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to </code><code>catch</code> <code>this</code> <code>in the debugger.</code>
<code>The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.</code>
本文轉自Fly_Elephant部落格園部落格,原文連結:http://www.cnblogs.com/xiaofeixiang/p/4612683.html,如需轉載請自行聯系原作者