欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Objective-C的UIStackView常用屬性函數(shù)學(xué)習(xí)筆記

 更新時間:2023年03月30日 09:52:43   作者:無聲編碼器  
這篇文章主要為大家介紹了Objective-C的UIStackView常用屬性函數(shù)學(xué)習(xí)筆記,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

UIStackView

UIStackView能夠利用自動布局的功能,創(chuàng)建能夠動態(tài)適應(yīng)設(shè)備方向、屏幕大小和可用空間中任何更改的用戶界面。

UIStackView管理其arrangedSubviews屬性中所有視圖的布局。這些視圖是根據(jù)它們在arrangedSubviews數(shù)組中的順序沿堆棧視圖的軸線排列的。具體布局因UIStackView的軸線、分布、對齊、間距和其他特性而異。

我們負(fù)責(zé)定義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:@"藍(lán)色按鈕" 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],
    ]];
}

顯示如下:

當(dāng)設(shè)置藍(lán)色視圖隱藏時,顯示如下:

當(dāng)修改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],
 ]];

當(dāng)修改子視圖約束,限制子視圖大小時,顯示如下:

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進(jìn)行調(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:@"藍(lán)色按鈕" 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,如果我們嚴(yán)格限制UIStackView的約束,就應(yīng)當(dāng)給予UIStackView自動調(diào)整其子視圖約束的權(quán)力,如果我們嚴(yán)格限制其子視圖約束,就應(yīng)當(dāng)給予UIStackView自動調(diào)整自身約束的權(quán)力,如果我們既嚴(yán)格限制UIStackView的約束,又嚴(yán)格限制其子視圖約束,我們會得到約束沖突,這是來自UIStackView的抗議。

常用屬性

@property(nonatomic) UILayoutConstraintAxis axis;

屬性描述設(shè)置UIStackView排列視圖時所沿的軸線方向。UILayoutConstraintAxis提供了兩個枚舉值,UILayoutConstraintAxisHorizontal(水平排列)與UILayoutConstraintAxisVertical(垂直排列),默認(rèn)為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的軸線方向填充可用空間。
       當(dāng)排列的視圖不適合(上溢) 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的軸線方向填充可用空間。
       當(dāng)排列的視圖沒有填充UIStackView時(下溢),它會均勻地填充視圖之間的間距。
       如果排列的視圖不適合UIStackView時(上溢),它會根據(jù)視圖的抗壓優(yōu)先級縮小視圖。
       如果存在任何歧義,堆棧視圖將根據(jù)視圖在arrangedSubviews數(shù)組中的索引縮小視圖。
       即子視圖等間距填充UIStackView,間距為UIStackView調(diào)整,spacing屬性限制了最小間距,但不限制最大間距。
     */
    UIStackViewDistributionEqualSpacing,
    
    /* 一種布局,試圖定位排列視圖,使其沿UIStackView的軸線方向具以相等的中心間距填充可用空間。
        當(dāng)排列的視圖沒有填充UIStackView時(下溢),如果未設(shè)置spacing屬性,則自動插入間距,并調(diào)整間距以滿足排列的子視圖有相等的中心間距。
        如果設(shè)置spacing屬性,在維持以spacing屬性設(shè)置間距值為最小間距的同時,它會根據(jù)視圖的抗壓優(yōu)先級縮小視圖以滿足排列的子視圖有相等的中心間距。
        當(dāng)排列的視圖不適合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布局的排列視圖之間的嚴(yán)格間距,UIStackViewDistributionEqualSpace和UIStackViewDistributionEqualCenter布局的最小間距。使用負(fù)值允許重疊。默認(rèn)值為0.0。

@property(nonatomic,getter=isBaselineRelativeArrangement) BOOL baselineRelativeArrangement;

屬性描述一個布爾值,默認(rèn)值為NO,用于確定是否從視圖的基線測量視圖之間的垂直間距。如果為YES,視圖之間的垂直間距將從基于文本的視圖的最后一條基線到其下方視圖的第一條基線進(jìn)行測量。頂部和底部視圖的定位也使其最近的基線距離堆棧視圖的邊緣指定的距離。此屬性僅由垂直排列的UIStackView視圖使用。水平排列的UIStackView可以使用alignment屬性控制。

@property(nonatomic,getter=isLayoutMarginsRelativeArrangement) BOOL layoutMarginsRelativeArrangement; 

屬性描述如果為YES,UIStackView將相對于其布局邊距布局其排列視圖。如果為NO,它將相對于其邊界布置排列的視圖。默認(rèn)為NO。

@property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *arrangedSubviews;

屬性描述由UIStackView排列的視圖數(shù)組。UIStackView確保了arrangedSubviews數(shù)組總是它的子視圖數(shù)組(subviews)的一個子集。每當(dāng)調(diào)用addArrangedSubview:方法時,如果尚未添加該子視圖,UIStackView都會將該視圖添加為子視圖,每當(dāng)調(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ù)組中當(dāng)前的視圖數(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í)筆記的詳細(xì)內(nèi)容,更多關(guān)于Objective-C UIStackView的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • iOS中表單列表樣式鍵盤遮擋的解決方案

    iOS中表單列表樣式鍵盤遮擋的解決方案

    這篇文章主要給大家介紹了關(guān)于iOS中表單列表樣式鍵盤遮擋的解決方案,文中通過示例代碼將解決的方法一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧
    2019-01-01
  • iOS應(yīng)用開發(fā)中使用NSLocale類實現(xiàn)對象信息的本地化

    iOS應(yīng)用開發(fā)中使用NSLocale類實現(xiàn)對象信息的本地化

    這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用NSLocale類實現(xiàn)對象信息的本地化的方法,能夠?qū)r間和貨幣等格式化為與系統(tǒng)本地設(shè)置相同的偏好,需要的朋友可以參考下
    2016-05-05
  • iOS自定義日期、時間、城市選擇器實例代碼

    iOS自定義日期、時間、城市選擇器實例代碼

    這篇文章主要介紹了iOS自定義日期、時間、城市選擇器實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • iOS?Segment帶滑動條切換效果

    iOS?Segment帶滑動條切換效果

    這篇文章主要為大家詳細(xì)介紹了iOS?Segment帶滑動條切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Swift中的HTTP請求體Request Bodies使用示例詳解

    Swift中的HTTP請求體Request Bodies使用示例詳解

    這篇文章主要為大家介紹了Swift中的HTTP請求體Request Bodies使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 解決iOS驗證碼顯示在左邊問題

    解決iOS驗證碼顯示在左邊問題

    這篇文章主要介紹了iOS驗證碼顯示在左邊問題,本文給大家分享解決思路通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • Flutter?ScrollController滾動監(jiān)聽及控制示例詳解

    Flutter?ScrollController滾動監(jiān)聽及控制示例詳解

    這篇文章主要為大家介紹了Flutter?ScrollController滾動監(jiān)聽及控制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • iOS 加載Bundle文件的實例代碼

    iOS 加載Bundle文件的實例代碼

    這篇文章主要介紹了iOS 加載Bundle文件的實例代碼,代碼簡單易懂,非常不錯,需要的朋友參考下
    2016-12-12
  • iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)

    iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)

    UILabel組件可以用來設(shè)置文字內(nèi)容的排版與字體效果等,功能非常多,下面就來為大家整理一下基本的iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)
    2016-05-05
  • 解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題

    解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題

    今天小編就為大家分享一篇解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評論