日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS: 在代码中使用Autolayout (1) - 按比例缩放和优先级

發(fā)布時間:2024/9/30 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS: 在代码中使用Autolayout (1) - 按比例缩放和优先级 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先說按比例縮放,這是在Interface Builder中無法設(shè)置的內(nèi)容。而在代碼中,使用NSLayoutConstraint類型的初始化函數(shù)中的multiplier參數(shù)就可以非常簡單的設(shè)置按比例縮放。同時也可以設(shè)置不同NSLayoutAttribute參數(shù)來達到意想不到的效果,比如“A的Width等于B的Height的2倍”這樣的效果。

OK,開始寫代碼,我們就拿一個簡單的UIButton做示例,在ViewController中創(chuàng)建一個UIButton字段:

UIButton?*btn;

命令這個Button水平居中,始終距離父View底部20單位的距離。然后高度是父View高度的三分之一。

最后使用KVO來監(jiān)控Button的大小并實時輸出到屏幕上。

代碼:

- (void)viewDidLoad

{

??? [super?viewDidLoad];

???

????//創(chuàng)建UIButton,不需要設(shè)置frame

????btn?= [UIButton?buttonWithType:UIButtonTypeRoundedRect];

??? [btn?setTitle:@"mgen"?forState:UIControlStateNormal];

????btn.backgroundColor?= [UIColor?greenColor];

??? [self.view?addSubview:btn];

????//禁止自動轉(zhuǎn)換AutoresizingMask

????btn.translatesAutoresizingMaskIntoConstraints?=?NO;

???

????//居中

??? [self.view?addConstraint:[NSLayoutConstraint

??????????????????????????????constraintWithItem:btn

??????????????????????????????attribute:NSLayoutAttributeCenterX

??????????????????????????????relatedBy:NSLayoutRelationEqual

??????????????????????????????toItem:self.view

??????????????????????????????attribute:NSLayoutAttributeCenterX

??????????????????????????????multiplier:1

??????????????????????????????constant:0]];

???

????//距離底部20單位

????//注意NSLayoutConstraint創(chuàng)建的constant是加在toItem參數(shù)的,所以需要-20。

??? [self.view?addConstraint:[NSLayoutConstraint

??????????????????????????????constraintWithItem:btn

??????????????????????????????attribute:NSLayoutAttributeBottom

??????????????????????????????relatedBy:NSLayoutRelationEqual

??????????????????????????????toItem:self.view

??????????????????????????????attribute:NSLayoutAttributeBottom

??????????????????????????????multiplier:1

??????????????????????????????constant:-20]];

???

????//定義高度是父View的三分之一

??? [self.view?addConstraint:[NSLayoutConstraint

??????????????????????????????constraintWithItem:btn

??????????????????????????????attribute:NSLayoutAttributeHeight

??????????????????????????????relatedBy:NSLayoutRelationEqual

??????????????????????????????toItem:self.view

??????????????????????????????attribute:NSLayoutAttributeHeight

??????????????????????????????multiplier:0.3

??????????????????????????????constant:0]];

???

????//注冊KVO方法

??? [btn?addObserver:self?forKeyPath:@"bounds"?options:NSKeyValueObservingOptionNew?|NSKeyValueObservingOptionInitial?context:nil];???

}

?

//KVO回調(diào)

- (void)observeValueForKeyPath:(NSString?*)keyPath ofObject:(id)object change:(NSDictionary?*)change context:(void?*)context

{

????if?(object ==?btn?&& [keyPath?isEqualToString:@"bounds"])

??? {

??????? [btn?setTitle:NSStringFromCGSize(btn.bounds.size)?forState:UIControlStateNormal];

??? }

}

?

運行結(jié)果:

?

?

OK,沒有任何問題。

?

接下來有一個新的需求,在橫向的顯示中,Button的高度只有96,覺得他太短了,所以要求Button的最小高度為150。

這樣的話,需要加入另一個限制大小的Constraint,但是這兩個Constraint在某些情況下是有沖突的,我們可以通過設(shè)置Constraint的優(yōu)先級來解決。優(yōu)先級對應NSLayoutConstraint類型的priority屬性,默認值是UILayoutPriorityRequired,數(shù)值上等于1000. 設(shè)置一個低的值代表更低的優(yōu)先級。

另外對于最小值的定義,使用NSLayoutRelationGreaterThanOrEqual作為NSLayoutConstraint類型創(chuàng)建時的relatedBy參數(shù)。

?

修改上面的比例Constraint,并在下方加入一個新的限制最小值的Constraint,代碼:

//定義高度是父View的三分之一

//設(shè)置優(yōu)先級低于UILayoutPriorityRequired(1000),UILayoutPriorityDefaultHigh是750

NSLayoutConstraint?*con = [NSLayoutConstraint

??????????????????????????constraintWithItem:btn

??????????????????????????attribute:NSLayoutAttributeHeight

??????????????????????????relatedBy:NSLayoutRelationEqual

??????????????????????????toItem:self.view

??????????????????????????attribute:NSLayoutAttributeHeight

??????????????????????????multiplier:0.3

??????????????????????????constant:0];

con.priority?=?UILayoutPriorityDefaultHigh;

[self.view?addConstraint:con];

?

//設(shè)置btn最小高度為150

[btn?addConstraint:[NSLayoutConstraint

????????????????????constraintWithItem:btn

????????????????????attribute:NSLayoutAttributeHeight

????????????????????relatedBy:NSLayoutRelationGreaterThanOrEqual

????????????????????toItem:nil

????????????????????attribute:NSLayoutAttributeNotAnAttribute

????????????????????multiplier:1

????????????????????constant:150]];

?

運行后,橫向屏幕中的Button高度成了150:

?

總結(jié)

以上是生活随笔為你收集整理的iOS: 在代码中使用Autolayout (1) - 按比例缩放和优先级的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。