溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Objective-C的UIStackView常用屬性函數(shù)有哪些

發(fā)布時(shí)間:2023-03-30 11:09:33 來源:億速云 閱讀:89 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Objective-C的UIStackView常用屬性函數(shù)有哪些”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

UIStackView

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

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

我們負(fù)責(zé)定義UIStackView的位置和大小(可選),UIStackView管理其內(nèi)容的布局和大小。

UIStackView使用的簡(jiǎn)單示例:\color{red}{UIStackView使用的簡(jiǎn)單示例 :}UIStackView使用的簡(jiǎn)單示例:

- (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],
    ]];
}

顯示如下:

Objective-C的UIStackView常用屬性函數(shù)有哪些

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

Objective-C的UIStackView常用屬性函數(shù)有哪些

當(dāng)修改UIStackView約束,限制UIStackView大小時(shí),顯示如下:

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],
 ]];

Objective-C的UIStackView常用屬性函數(shù)有哪些

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

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],
    ]];

Objective-C的UIStackView常用屬性函數(shù)有哪些

既限制UIStackView約束,又限制子視圖約束時(shí),至少有一個(gè)子視圖可以由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,
    ]];
}

Objective-C的UIStackView常用屬性函數(shù)有哪些

UIStackView就像一個(gè)自動(dòng)適應(yīng)其子視圖約束或管理其子視圖約束的容器視圖,可以大量的節(jié)省設(shè)置或更新約束的代碼。我們需要在某一方面放權(quán)給UIStackView,如果我們嚴(yán)格限制UIStackView的約束,就應(yīng)當(dāng)給予UIStackView自動(dòng)調(diào)整其子視圖約束的權(quán)力,如果我們嚴(yán)格限制其子視圖約束,就應(yīng)當(dāng)給予UIStackView自動(dòng)調(diào)整自身約束的權(quán)力,如果我們既嚴(yán)格限制UIStackView的約束,又嚴(yán)格限制其子視圖約束,我們會(huì)得到約束沖突,這是來自UIStackView的抗議。

常用屬性

@property(nonatomic) UILayoutConstraintAxis axis;

屬性描述設(shè)置UIStackView排列視圖時(shí)所沿的軸線方向。UILayoutConstraintAxis提供了兩個(gè)枚舉值,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時(shí),它會(huì)根據(jù)視圖的抗壓優(yōu)先級(jí)縮小視圖。
       如果排列的視圖沒有填充(下溢) UIStackView時(shí),它會(huì)根據(jù)視圖的擁抱優(yōu)先級(jí)拉伸視圖。
       如果存在任何歧義,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時(shí)(下溢),它會(huì)均勻地填充視圖之間的間距。
       如果排列的視圖不適合UIStackView時(shí)(上溢),它會(huì)根據(jù)視圖的抗壓優(yōu)先級(jí)縮小視圖。
       如果存在任何歧義,堆棧視圖將根據(jù)視圖在arrangedSubviews數(shù)組中的索引縮小視圖。
       即子視圖等間距填充UIStackView,間距為UIStackView調(diào)整,spacing屬性限制了最小間距,但不限制最大間距。
     */
    UIStackViewDistributionEqualSpacing,
    
    /* 一種布局,試圖定位排列視圖,使其沿UIStackView的軸線方向具以相等的中心間距填充可用空間。
        當(dāng)排列的視圖沒有填充UIStackView時(shí)(下溢),如果未設(shè)置spacing屬性,則自動(dòng)插入間距,并調(diào)整間距以滿足排列的子視圖有相等的中心間距。
        如果設(shè)置spacing屬性,在維持以spacing屬性設(shè)置間距值為最小間距的同時(shí),它會(huì)根據(jù)視圖的抗壓優(yōu)先級(jí)縮小視圖以滿足排列的子視圖有相等的中心間距。
        當(dāng)排列的視圖不適合UIStackView時(shí)(上溢),如果未設(shè)置spacing屬性,它會(huì)根據(jù)視圖的抗壓優(yōu)先級(jí)縮小視圖以滿足排列的子視圖有相等的中心間距。
        如果設(shè)置spacing屬性,在維持以spacing屬性設(shè)置間距值為最小間距的同時(shí),它會(huì)根據(jù)視圖的抗壓優(yōu)先級(jí)縮小視圖以滿足排列的子視圖有相等的中心間距。
        即子視圖等中心間距填充UIStackView,間距為UIStackView調(diào)整,spacing屬性限制了最小間距,但不限制最大間距。
     */
    UIStackViewDistributionEqualCentering,
} API_AVAILABLE(ios(9.0));
@property(nonatomic) UIStackViewAlignment alignment;

屬性描述UIStackView排列的子視圖的對(duì)齊方式,其對(duì)齊方式受UIStackView排列視圖時(shí)所沿的軸線方向影響。

  • UIStackViewAlignment提供的對(duì)齊子視圖的方式如下:

typedef NS_ENUM(NSInteger, UIStackViewAlignment) {
    /* 填充式布局,如果UIStackView為水平排列,則子視圖頂部與底部對(duì)齊UIStackView。
       如果UIStackView為垂直排列,則子視圖前部與后部對(duì)齊UIStackView。
     */
    UIStackViewAlignmentFill,
    
    /* 前部對(duì)齊式布局,UIStackView為垂直排列有效。
     */
    UIStackViewAlignmentLeading,
    /* 頂部對(duì)齊式布局,UIStackView為水平排列有效。
     */
    UIStackViewAlignmentTop = UIStackViewAlignmentLeading,
    /*按照第一個(gè)子視圖的第一行文文本基線對(duì)齊,且高度最大的子視圖底部對(duì)齊。UIStackView為水平排列有效。
    */
    UIStackViewAlignmentFirstBaseline, 
    
    /* 子視圖沿軸線方向劇中對(duì)齊
     */
    UIStackViewAlignmentCenter,
    
    /* 后部對(duì)齊式布局,UIStackView為垂直排列有效。
     */
    UIStackViewAlignmentTrailing,
    /*底部對(duì)齊式布局,UIStackView為水平排列有效。
    */
    UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing,
    /*按照第一個(gè)子視圖的最后一行文文本基線對(duì)齊,且高度最大的子視圖頂部對(duì)齊。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;

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

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

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

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

屬性描述由UIStackView排列的視圖數(shù)組。UIStackView確保了arrangedSubviews數(shù)組總是它的子視圖數(shù)組(subviews)的一個(gè)子集。每當(dāng)調(diào)用addArrangedSubview:方法時(shí),如果尚未添加該子視圖,UIStackView都會(huì)將該視圖添加為子視圖,每當(dāng)調(diào)用removeFromSuperview:方法時(shí),UIStackView也會(huì)將其從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對(duì)象。UIStackView將所有需要排列的視圖添加到其arrangedSubviews組中,并這些視圖添加為子視圖。如果arrangedSubviews數(shù)組中包含的任何視圖收到removeFromSuperview的方法調(diào)用,UIStackView也會(huì)將其從arrangedSubviews中刪除。

參數(shù) :

views :要由UIStackView排列的視圖數(shù)組。

返回值 : 一個(gè)新的UIStackView對(duì)象。

- (void)addArrangedSubview:(UIView *)view;

函數(shù)描述將視圖添加到arrangedSubviews數(shù)組的末尾。UIStackView確保了arrangedSubviews數(shù)組總是它的子視圖數(shù)組(subviews)的一個(gè)子集。如果尚未添加該子視圖,此方法會(huì)自動(dòng)將提供的視圖添加為UIStackView的子視圖,如果已經(jīng)添加該子視圖,此函數(shù)不做操作。

參數(shù) :

view : 要添加到由UIStackView排列的視圖數(shù)組中的視圖。

- (void)removeArrangedSubview:(UIView *)view;

函數(shù)描述此方法從UIStackView的arrangedSubviews數(shù)組中刪除提供的視圖。視圖的位置和大小將不再由堆棧視圖管理。但是,此方法不會(huì)從堆棧的子視圖數(shù)組中(subviews)刪除提供的視圖,因此視圖仍然顯示為視圖層次的一部分,視圖仍顯示在屏幕上,需要通過調(diào)用視圖的removeFromSuperview方法從子視圖數(shù)組中顯式刪除視圖。

- (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex;

函數(shù)描述將提供的視圖添加到排列的子視圖數(shù)組中指定的索引處。如果索引已被占用,UIStackView會(huì)增加arrangedSubviews數(shù)組的大小,并將其被占用的索引及被占用的索引以上位置的所有內(nèi)容移動(dòng)到數(shù)組中更高的空間(索引后移),然后UIStackView將提供的視圖存儲(chǔ)在索引處。如果插入的視圖尚未添加到UIStackView,此方法會(huì)自動(dòng)將提供的視圖添加為UIStackView的子視圖,但插入的索引位置僅影響arrangedSubviews數(shù)組中視圖的順序,它不會(huì)影響子視圖數(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ù)描述 :水平排列時(shí),在指定視圖后緣應(yīng)用自定義間距。垂直排列時(shí),在指定視圖下緣應(yīng)用自定義間距。

參數(shù) :

spacing : 自定義間距。

arrangedSubview : 指定視圖。

- (CGFloat)customSpacingAfterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));

函數(shù)描述 :水平排列時(shí),返回指定視圖后緣應(yīng)用自定義間距。垂直排列時(shí),返回指定視圖下緣自定義間距。

參數(shù) :

arrangedSubview : 指定視圖。

“Objective-C的UIStackView常用屬性函數(shù)有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI