Objective-C的UIStackView常用屬性函數(shù)學(xué)習(xí)筆記
UIStackView
UIStackView能夠利用自動布局的功能,創(chuàng)建能夠動態(tài)適應(yīng)設(shè)備方向、屏幕大小和可用空間中任何更改的用戶界面。
UIStackView管理其arrangedSubviews屬性中所有視圖的布局。這些視圖是根據(jù)它們在arrangedSubviews數(shù)組中的順序沿堆棧視圖的軸線排列的。具體布局因UIStackView的軸線、分布、對齊、間距和其他特性而異。
我們負責定義UIStackView的位置和大小(可選),UIStackView管理其內(nèi)容的布局和大小。
UIStackView使用的簡單示例:\color{red}{UIStackView使用的簡單示例 :}UIStackView使用的簡單示例:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *redButton = [UIButton buttonWithType:UIButtonTypeCustom];
[redButton setTitle:@"紅色按鈕" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
UIButton *greenButton = [UIButton buttonWithType:UIButtonTypeCustom];
[greenButton setTitle:@"綠色按鈕" forState:UIControlStateNormal];
greenButton.backgroundColor = [UIColor greenColor];
UIButton *blueButton = [UIButton buttonWithType:UIButtonTypeCustom];
[blueButton setTitle:@"藍色按鈕" forState:UIControlStateNormal];
blueButton.backgroundColor = [UIColor blueColor];
UIStackView *stackView = [[UIStackView alloc]initWithArrangedSubviews:@[redButton,greenButton,blueButton]];
stackView.backgroundColor = [UIColor yellowColor];
stackView.alignment = UIStackViewAlignmentCenter;
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFill;
[self.view addSubview:stackView];
stackView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:@[
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
]];
}
顯示如下:

當設(shè)置藍色視圖隱藏時,顯示如下:

當修改UIStackView約束,限制UIStackView大小時,顯示如下:
stackView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:@[
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[stackView.heightAnchor constraintEqualToConstant:100],
[stackView.widthAnchor constraintEqualToConstant:300],
]];

當修改子視圖約束,限制子視圖大小時,顯示如下:
stackView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:@[
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
]];
redButton.translatesAutoresizingMaskIntoConstraints = NO;
[stackView addConstraints:@[
[redButton.heightAnchor constraintEqualToConstant:50],
[redButton.widthAnchor constraintEqualToConstant:100],
]];
greenButton.translatesAutoresizingMaskIntoConstraints = NO;
[stackView addConstraints:@[
[greenButton.heightAnchor constraintEqualToConstant:50],
[greenButton.widthAnchor constraintEqualToConstant:80],
]];
blueButton.translatesAutoresizingMaskIntoConstraints = NO;
[stackView addConstraints:@[
[blueButton.heightAnchor constraintEqualToConstant:50],
[blueButton.widthAnchor constraintEqualToConstant:120],
]];

既限制UIStackView約束,又限制子視圖約束時,至少有一個子視圖可以由UIStackView進行調(diào)整,顯示如下:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *redButton = [UIButton buttonWithType:UIButtonTypeCustom];
[redButton setTitle:@"紅色按鈕" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
UIButton *greenButton = [UIButton buttonWithType:UIButtonTypeCustom];
[greenButton setTitle:@"綠色按鈕" forState:UIControlStateNormal];
greenButton.backgroundColor = [UIColor greenColor];
UIButton *blueButton = [UIButton buttonWithType:UIButtonTypeCustom];
[blueButton setTitle:@"藍色按鈕" forState:UIControlStateNormal];
blueButton.backgroundColor = [UIColor blueColor];
UIStackView *stackView = [[UIStackView alloc]initWithArrangedSubviews:@[redButton,greenButton,blueButton]];
stackView.backgroundColor = [UIColor yellowColor];
stackView.alignment = UIStackViewAlignmentCenter;
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFill;
[self.view addSubview:stackView];
stackView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:@[
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[stackView.heightAnchor constraintEqualToConstant:100],
[stackView.widthAnchor constraintEqualToConstant:200],
]];
redButton.translatesAutoresizingMaskIntoConstraints = NO;
[stackView addConstraints:@[
[redButton.heightAnchor constraintEqualToConstant:50],
[redButton.widthAnchor constraintEqualToConstant:80],
]];
greenButton.translatesAutoresizingMaskIntoConstraints = NO;
[stackView addConstraints:@[
[greenButton.heightAnchor constraintEqualToConstant:50],
[greenButton.widthAnchor constraintEqualToConstant:80],
]];
blueButton.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *blueButtonWidthAnchor = [blueButton.widthAnchor constraintEqualToConstant:120];
blueButtonWidthAnchor.priority = UILayoutPriorityDefaultLow;
[stackView addConstraints:@[
[blueButton.heightAnchor constraintEqualToConstant:50],
blueButtonWidthAnchor,
]];
}

UIStackView就像一個自動適應(yīng)其子視圖約束或管理其子視圖約束的容器視圖,可以大量的節(jié)省設(shè)置或更新約束的代碼。我們需要在某一方面放權(quán)給UIStackView,如果我們嚴格限制UIStackView的約束,就應(yīng)當給予UIStackView自動調(diào)整其子視圖約束的權(quán)力,如果我們嚴格限制其子視圖約束,就應(yīng)當給予UIStackView自動調(diào)整自身約束的權(quán)力,如果我們既嚴格限制UIStackView的約束,又嚴格限制其子視圖約束,我們會得到約束沖突,這是來自UIStackView的抗議。
常用屬性
@property(nonatomic) UILayoutConstraintAxis axis;
屬性描述 :設(shè)置UIStackView排列視圖時所沿的軸線方向。UILayoutConstraintAxis提供了兩個枚舉值,UILayoutConstraintAxisHorizontal(水平排列)與UILayoutConstraintAxisVertical(垂直排列),默認為UILayoutConstraintAxisHorizontal。
- UILayoutConstraintAxis提供的枚舉值:
typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) {
//水平排列
UILayoutConstraintAxisHorizontal = 0,
//垂直排列
UILayoutConstraintAxisVertical = 1
};
@property(nonatomic) UIStackViewDistribution distribution;
屬性描述 : 設(shè)置UIStackView沿指定軸線方向布局子視圖的方式。
- UIStackViewDistribution提供的布局子視圖的方式如下:
typedef NS_ENUM(NSInteger, UIStackViewDistribution) {
/* 一種布局,其中UIStackView調(diào)整其排列的視圖的大小,以便它們沿著UIStackView的軸線方向填充可用空間。
當排列的視圖不適合(上溢) UIStackView時,它會根據(jù)視圖的抗壓優(yōu)先級縮小視圖。
如果排列的視圖沒有填充(下溢) UIStackView時,它會根據(jù)視圖的擁抱優(yōu)先級拉伸視圖。
如果存在任何歧義,UIStackView將根據(jù)排列的子視圖數(shù)組中的索引調(diào)整排列的視圖的大小。
即將UIStackView填充滿。
*/
UIStackViewDistributionFill = 0,
/*一種布局,其中堆棧視圖調(diào)整其排列視圖的大小,以便它們沿著UIStackView的軸線方向均勻的填充可用空間。
即子視圖以相同大小填充UIStackView
*/
UIStackViewDistributionFillEqually,
/* 一種布局,其中堆棧視圖調(diào)整其排列視圖的大小,以便它們沿著UIStackView的軸線方向按比例調(diào)整大小填充可用空。
即子視圖以比例大小填充UIStackView。
*/
UIStackViewDistributionFillProportionally,
/* 一種布局,其中UIStackView定位其排列視圖,以便它們沿著UIStackView的軸線方向填充可用空間。
當排列的視圖沒有填充UIStackView時(下溢),它會均勻地填充視圖之間的間距。
如果排列的視圖不適合UIStackView時(上溢),它會根據(jù)視圖的抗壓優(yōu)先級縮小視圖。
如果存在任何歧義,堆棧視圖將根據(jù)視圖在arrangedSubviews數(shù)組中的索引縮小視圖。
即子視圖等間距填充UIStackView,間距為UIStackView調(diào)整,spacing屬性限制了最小間距,但不限制最大間距。
*/
UIStackViewDistributionEqualSpacing,
/* 一種布局,試圖定位排列視圖,使其沿UIStackView的軸線方向具以相等的中心間距填充可用空間。
當排列的視圖沒有填充UIStackView時(下溢),如果未設(shè)置spacing屬性,則自動插入間距,并調(diào)整間距以滿足排列的子視圖有相等的中心間距。
如果設(shè)置spacing屬性,在維持以spacing屬性設(shè)置間距值為最小間距的同時,它會根據(jù)視圖的抗壓優(yōu)先級縮小視圖以滿足排列的子視圖有相等的中心間距。
當排列的視圖不適合UIStackView時(上溢),如果未設(shè)置spacing屬性,它會根據(jù)視圖的抗壓優(yōu)先級縮小視圖以滿足排列的子視圖有相等的中心間距。
如果設(shè)置spacing屬性,在維持以spacing屬性設(shè)置間距值為最小間距的同時,它會根據(jù)視圖的抗壓優(yōu)先級縮小視圖以滿足排列的子視圖有相等的中心間距。
即子視圖等中心間距填充UIStackView,間距為UIStackView調(diào)整,spacing屬性限制了最小間距,但不限制最大間距。
*/
UIStackViewDistributionEqualCentering,
} API_AVAILABLE(ios(9.0));
@property(nonatomic) UIStackViewAlignment alignment;
屬性描述 :UIStackView排列的子視圖的對齊方式,其對齊方式受UIStackView排列視圖時所沿的軸線方向影響。
- UIStackViewAlignment提供的對齊子視圖的方式如下:
typedef NS_ENUM(NSInteger, UIStackViewAlignment) {
/* 填充式布局,如果UIStackView為水平排列,則子視圖頂部與底部對齊UIStackView。
如果UIStackView為垂直排列,則子視圖前部與后部對齊UIStackView。
*/
UIStackViewAlignmentFill,
/* 前部對齊式布局,UIStackView為垂直排列有效。
*/
UIStackViewAlignmentLeading,
/* 頂部對齊式布局,UIStackView為水平排列有效。
*/
UIStackViewAlignmentTop = UIStackViewAlignmentLeading,
/*按照第一個子視圖的第一行文文本基線對齊,且高度最大的子視圖底部對齊。UIStackView為水平排列有效。
*/
UIStackViewAlignmentFirstBaseline,
/* 子視圖沿軸線方向劇中對齊
*/
UIStackViewAlignmentCenter,
/* 后部對齊式布局,UIStackView為垂直排列有效。
*/
UIStackViewAlignmentTrailing,
/*底部對齊式布局,UIStackView為水平排列有效。
*/
UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing,
/*按照第一個子視圖的最后一行文文本基線對齊,且高度最大的子視圖頂部對齊。UIStackView為水平排列有效。
*/
UIStackViewAlignmentLastBaseline, // Valid for horizontal axis only
} API_AVAILABLE(ios(9.0));
@property(nonatomic) CGFloat spacing;
屬性描述 :UIStackView排列子視圖相鄰邊之間的間距。此屬性定義了UIStackViewDistributionFill、UIStackViewDistributionFillEqually、UIStackViewDistributionFillProportionally布局的排列視圖之間的嚴格間距,UIStackViewDistributionEqualSpace和UIStackViewDistributionEqualCenter布局的最小間距。使用負值允許重疊。默認值為0.0。
@property(nonatomic,getter=isBaselineRelativeArrangement) BOOL baselineRelativeArrangement;
屬性描述 :一個布爾值,默認值為NO,用于確定是否從視圖的基線測量視圖之間的垂直間距。如果為YES,視圖之間的垂直間距將從基于文本的視圖的最后一條基線到其下方視圖的第一條基線進行測量。頂部和底部視圖的定位也使其最近的基線距離堆棧視圖的邊緣指定的距離。此屬性僅由垂直排列的UIStackView視圖使用。水平排列的UIStackView可以使用alignment屬性控制。
@property(nonatomic,getter=isLayoutMarginsRelativeArrangement) BOOL layoutMarginsRelativeArrangement;
屬性描述 :如果為YES,UIStackView將相對于其布局邊距布局其排列視圖。如果為NO,它將相對于其邊界布置排列的視圖。默認為NO。
@property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *arrangedSubviews;
屬性描述 :由UIStackView排列的視圖數(shù)組。UIStackView確保了arrangedSubviews數(shù)組總是它的子視圖數(shù)組(subviews)的一個子集。每當調(diào)用addArrangedSubview:方法時,如果尚未添加該子視圖,UIStackView都會將該視圖添加為子視圖,每當調(diào)用removeFromSuperview:方法時,UIStackView也會將其從arrangedSubviews中刪除。
常用函數(shù)
- (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER; - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
函數(shù)描述 :初始化UIStackView。
- (instancetype)initWithArrangedSubviews:(NSArray<__kindof UIView *> *)views;
函數(shù)描述 :返回管理所提供的視圖的UIStackView對象。UIStackView將所有需要排列的視圖添加到其arrangedSubviews組中,并這些視圖添加為子視圖。如果arrangedSubviews數(shù)組中包含的任何視圖收到removeFromSuperview的方法調(diào)用,UIStackView也會將其從arrangedSubviews中刪除。
參數(shù) :
views :要由UIStackView排列的視圖數(shù)組。
返回值 : 一個新的UIStackView對象。
- (void)addArrangedSubview:(UIView *)view;
函數(shù)描述 :將視圖添加到arrangedSubviews數(shù)組的末尾。UIStackView確保了arrangedSubviews數(shù)組總是它的子視圖數(shù)組(subviews)的一個子集。如果尚未添加該子視圖,此方法會自動將提供的視圖添加為UIStackView的子視圖,如果已經(jīng)添加該子視圖,此函數(shù)不做操作。
參數(shù) :
view : 要添加到由UIStackView排列的視圖數(shù)組中的視圖。
- (void)removeArrangedSubview:(UIView *)view;
函數(shù)描述 :此方法從UIStackView的arrangedSubviews數(shù)組中刪除提供的視圖。視圖的位置和大小將不再由堆棧視圖管理。但是,此方法不會從堆棧的子視圖數(shù)組中(subviews)刪除提供的視圖,因此視圖仍然顯示為視圖層次的一部分,視圖仍顯示在屏幕上,需要通過調(diào)用視圖的removeFromSuperview方法從子視圖數(shù)組中顯式刪除視圖。
- (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex;
函數(shù)描述 :將提供的視圖添加到排列的子視圖數(shù)組中指定的索引處。如果索引已被占用,UIStackView會增加arrangedSubviews數(shù)組的大小,并將其被占用的索引及被占用的索引以上位置的所有內(nèi)容移動到數(shù)組中更高的空間(索引后移),然后UIStackView將提供的視圖存儲在索引處。如果插入的視圖尚未添加到UIStackView,此方法會自動將提供的視圖添加為UIStackView的子視圖,但插入的索引位置僅影響arrangedSubviews數(shù)組中視圖的順序,它不會影響子視圖數(shù)組(subviews)中視圖的順序,也就是說插入的視圖仍舊添加到子視圖數(shù)組(subviews)的末尾。
參數(shù) :
view : 要插入到由UIStackView排列的視圖數(shù)組中的視圖。
stackIndex : 其在arrangedSubviews數(shù)組中插入新視圖的索引,此值不得大于此數(shù)組中當前的視圖數(shù),如果索引超出范圍,此方法將拋出NSInternalInconsistencyException異常。
- (void)setCustomSpacing:(CGFloat)spacing afterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));
函數(shù)描述 :水平排列時,在指定視圖后緣應(yīng)用自定義間距。垂直排列時,在指定視圖下緣應(yīng)用自定義間距。
參數(shù) :
spacing : 自定義間距。
arrangedSubview : 指定視圖。
- (CGFloat)customSpacingAfterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));
函數(shù)描述 :水平排列時,返回指定視圖后緣應(yīng)用自定義間距。垂直排列時,返回指定視圖下緣自定義間距。
參數(shù) :
arrangedSubview : 指定視圖。
以上就是Objective-C的UIStackView常用屬性函數(shù)學(xué)習(xí)筆記的詳細內(nèi)容,更多關(guān)于Objective-C UIStackView的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS UICollectionView實現(xiàn)橫向滑動
這篇文章主要為大家詳細介紹了iOS UICollectionView實現(xiàn)橫向滑動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
IOS 通過tag刪除動態(tài)創(chuàng)建的UIButton
這篇文章主要介紹了IOS 通過tag刪除動態(tài)創(chuàng)建的UIButton的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS App開發(fā)中UITextField組件的常用屬性小結(jié)
這篇文章主要介紹了iOS App開發(fā)中UITextField組件的常用屬性小結(jié),文中還介紹了UITextField隱藏鍵盤及為內(nèi)容增加校驗的兩個使用技巧,需要的朋友可以參考下2016-04-04
iOS開發(fā)之image圖片壓縮及壓縮成指定大小的兩種方法
這篇文章主要介紹了iOS開發(fā)之image圖片壓縮及壓縮成指定大小的兩種方法,需要的朋友可以參考下2017-11-11
IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)
這篇文章主要介紹了IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS中模態(tài)Model視圖跳轉(zhuǎn)和Push視圖跳轉(zhuǎn)的需求實現(xiàn)方法
這篇文章主要介紹了iOS中模態(tài)Model視圖跳轉(zhuǎn)和Push視圖跳轉(zhuǎn)的需求實現(xiàn),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12
iOS開發(fā)中實現(xiàn)hook消息機制的方法探究
這篇文章主要介紹了iOS開發(fā)中實現(xiàn)hook消息機制的方法探究,這里用到了一個Method Swizzling原理,需要的朋友可以參考下2015-10-10

