xib相關(guān)(十三) —— UIStackView之枚舉UIStackViewDistribution使用(二)

版本記錄

版本號 時間
V1.0 2018.04.27

前言

iOS中的視圖加載可以有兩種方式,一種是通過xib加載,另外一種就是通過純代碼加載。它們各有優(yōu)點和好處,xib比較直觀簡單,代碼比較靈活但是看著很多很亂,上一家公司主要風(fēng)格就是用純代碼,這一家用的就是xib用的比較多。這幾篇我們就詳細的介紹一個xib相關(guān)知識。感興趣的可以看上面寫的幾篇。
1. xib相關(guān)(一) —— 基本知識(一)
2. xib相關(guān)(二) —— 文件沖突問題(一)
3. xib相關(guān)(三) —— xib右側(cè)標簽介紹(一)
4. xib相關(guān)(四) —— 連線問題(一)
5. xib相關(guān)(五) —— 利用layout進行約束之界面(一)
6. xib相關(guān)(六) —— 利用layout進行約束之說明和注意事項(二)
7. xib相關(guān)(七) —— Storyboard中的segue (一)
8. xib相關(guān)(八) —— Size Classes(一)
9. xib相關(guān)(九) —— 幾個IB修飾符(一)
10. xib相關(guān)(十) —— xib的國際化(一)
11. xib相關(guān)(十一) —— xib的高冷用法之修改視圖的圓角半徑、邊框?qū)挾群皖伾ㄒ唬?/a>
12. xib相關(guān)(十二) —— UIStackView之基本介紹(一)

API

下面看一下UIStackView的API文檔,這些其實在上一篇多少都有所介紹。

#import <UIKit/UIView.h>

/* Distribution—the layout along the stacking axis.
 
 All UIStackViewDistribution enum values fit first and last arranged subviews tightly to the container,
 and except for UIStackViewDistributionFillEqually, fit all items to intrinsicContentSize when possible.
 */
typedef NS_ENUM(NSInteger, UIStackViewDistribution) {
    
    /* When items do not fit (overflow) or fill (underflow) the space available
     adjustments occur according to compressionResistance or hugging
     priorities of items, or when that is ambiguous, according to arrangement
     order.
     */
    UIStackViewDistributionFill = 0,
    
    /* Items are all the same size.
     When space allows, this will be the size of the item with the largest
     intrinsicContentSize (along the axis of the stack).
     Overflow or underflow adjustments are distributed equally among the items.
     */
    UIStackViewDistributionFillEqually,
    
    /* Overflow or underflow adjustments are distributed among the items proportional
     to their intrinsicContentSizes.
     */
    UIStackViewDistributionFillProportionally,
    
    /* Additional underflow spacing is divided equally in the spaces between the items.
     Overflow squeezing is controlled by compressionResistance priorities followed by
     arrangement order.
     */
    UIStackViewDistributionEqualSpacing,
    
    /* Equal center-to-center spacing of the items is maintained as much
     as possible while still maintaining a minimum edge-to-edge spacing within the
     allowed area.
        Additional underflow spacing is divided equally in the spacing. Overflow 
     squeezing is distributed first according to compressionResistance priorities 
     of items, then according to subview order while maintaining the configured 
     (edge-to-edge) spacing as a minimum.
     */
    UIStackViewDistributionEqualCentering,
} NS_ENUM_AVAILABLE_IOS(9_0);

/* Alignment—the layout transverse to the stacking axis.
 */
typedef NS_ENUM(NSInteger, UIStackViewAlignment) {
    /* Align the leading and trailing edges of vertically stacked items
     or the top and bottom edges of horizontally stacked items tightly to the container.
     */
    UIStackViewAlignmentFill,
    
    /* Align the leading edges of vertically stacked items
     or the top edges of horizontally stacked items tightly to the relevant edge
     of the container
     */
    UIStackViewAlignmentLeading,
    UIStackViewAlignmentTop = UIStackViewAlignmentLeading,
    UIStackViewAlignmentFirstBaseline, // Valid for horizontal axis only
    
    /* Center the items in a vertical stack horizontally
     or the items in a horizontal stack vertically
     */
    UIStackViewAlignmentCenter,
    
    /* Align the trailing edges of vertically stacked items
     or the bottom edges of horizontally stacked items tightly to the relevant
     edge of the container
     */
    UIStackViewAlignmentTrailing,
    UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing,
    UIStackViewAlignmentLastBaseline, // Valid for horizontal axis only
} NS_ENUM_AVAILABLE_IOS(9_0);

/* Used when setting custom spacing after an arranged subview to indicate reverting to 
 the value specified by the spacing property. 
 
 See -setCustomSpacing:afterView:, -customSpacingAfterView:
 */
static const CGFloat UIStackViewSpacingUseDefault API_AVAILABLE(ios(11.0),tvos(11.0)) = FLT_MAX;

/* Used when setting custom spacing after an arranged subview to request the system 
 spacing to the neighboring view. 
 
 Also used as a token for the spacing property to request system spacing between
 arranged subviews.
 
 See spacing, -setCustomSpacing:afterView:, -customSpacingAfterView:
 */
static const CGFloat UIStackViewSpacingUseSystem API_AVAILABLE(ios(11.0),tvos(11.0)) = FLT_MIN;

/* UIStackView is a non-rendering subclass of UIView, intended for managing layout of its subviews.
 You may not override +[UIStackView layerClass], and -drawLayer:inContext: will not be sent to
 UIStackView.

 UIStackView arranges its subviews in its arrangedSubviews list in the order of 
 that list along a vertical or horizontal axis, with exact arrangement determined
 by the distribution, alignment, and spacing properties.
 
 The layout will update automatically when arrangedSubviews list changes
 due to adding, removing, or inserting arrangedSubviews, and also when
 views in the arrangedSubviews list change their hidden property.
 
 A horizontal UIStackView will return its tallest view for -viewForFirst/LastBaselineLayout,
 or if that is another stack view, then the relevant viewForFirst/LastBaselineLayout from that
 stack view.
 
 A vertical UIStackView will return its first view for -viewForFirstBaselineLayout and its
 last view for -viewForLastBaselineLayout, or if that is another stack view, then the relevant  
 viewForFirst/LastBaselineLayout from that stack view.
 */
NS_ASSUME_NONNULL_BEGIN
NS_CLASS_AVAILABLE_IOS(9_0)
@interface UIStackView : UIView

- (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;

/* UIStackView enforces that all views in the arrangedSubviews list
 must be subviews of the UIStackView.
    Thus, when a view is added to the arrangedSubviews, UIStackView
 adds it as a subview if it isn't already. And when a view in a
 UIStackView's arrangedSubviews list receives -removeFromSuperview
 it is also removed from the arrangedSubviews.
 
 Please note that this is a convenience initializer and cannot be overridden in Swift.
 */
- (instancetype)initWithArrangedSubviews:(NSArray<__kindof UIView *> *)views; // Adds views as subviews of the receiver.
@property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *arrangedSubviews;

/* Add a view to the end of the arrangedSubviews list.
 Maintains the rule that the arrangedSubviews list is a subset of the
 subviews list by adding the view as a subview of the receiver if
 necessary.
    Does not affect the subview ordering if view is already a subview 
 of the receiver.
 */
- (void)addArrangedSubview:(UIView *)view;

/* Removes a subview from the list of arranged subviews without removing it as
 a subview of the receiver.
    To remove the view as a subview, send it -removeFromSuperview as usual;
 the relevant UIStackView will remove it from its arrangedSubviews list
 automatically.
 */
- (void)removeArrangedSubview:(UIView *)view;
/*
 Adds the view as a subview of the container if it isn't already.
    Updates the stack index (but not the subview index) of the
 arranged subview if it's already in the arrangedSubviews list.
 */
- (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex;

/* A stack with a horizontal axis is a row of arrangedSubviews,
and a stack with a vertical axis is a column of arrangedSubviews.
 */
@property(nonatomic) UILayoutConstraintAxis axis;

/* The layout of the arrangedSubviews along the axis
 */
@property(nonatomic) UIStackViewDistribution distribution;

/* The layout of the arrangedSubviews transverse to the axis;
 e.g., leading/trailing edges in a vertical stack
 */
@property(nonatomic) UIStackViewAlignment alignment;

/* Spacing between adjacent edges of arrangedSubviews.
 Used as a strict spacing for the Fill distributions, and
 as a minimum spacing for the EqualCentering and EqualSpacing
 distributions. Use negative values to allow overlap.
 
 On iOS 11.0 or later, use UIStackViewSpacingUseSystem (Swift: UIStackView.spacingUseSystem) 
 to get a system standard spacing value. Setting spacing to UIStackViewSpacingUseDefault 
 (Swift: UIStackView.spacingUseDefault) will result in a spacing of 0.
 
 System spacing between views depends on the views involved, and may vary across the 
 stack view.
 
 In vertical stack views with baselineRelativeArrangement == YES, the spacing between 
 text-containing views (such as UILabels) will depend on the fonts involved.
 */
@property(nonatomic) CGFloat spacing;

/* Set and get custom spacing after a view. 
 
 This custom spacing takes precedence over any other value that might otherwise be used 
 for the space following the arranged subview.
 
 Defaults to UIStackViewSpacingUseDefault (Swift: UIStackView.spacingUseDefault), where 
 resolved value will match the spacing property.
 
 You may also set the custom spacing to UIStackViewSpacingUseSystem (Swift: UIStackView.spacingUseSystem),
 where the resolved value will match the system-defined value for the space to the neighboring view, 
 independent of the spacing property.
 
 Maintained when the arranged subview changes position in the stack view, but not after it
 is removed from the arrangedSubviews list.
 
 Ignored if arrangedSubview is not actually an arranged subview.
 */
- (void)setCustomSpacing:(CGFloat)spacing afterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));
- (CGFloat)customSpacingAfterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));

/* Baseline-to-baseline spacing in vertical stacks.
    The baselineRelativeArrangement property supports specifications of vertical 
 space from the last baseline of one text-based view to the first baseline of a
 text-based view below, or from the  top (or bottom) of a container to the first
 (or last) baseline of a contained text-based view.
    This property is ignored in horizontal stacks. Use the alignment property
 to specify baseline alignment in horizontal stacks.
    Defaults to NO.
 */
@property(nonatomic,getter=isBaselineRelativeArrangement) BOOL baselineRelativeArrangement;

/* Uses margin layout attributes for edge constraints where applicable.
    Defaults to NO.
 */
@property(nonatomic,getter=isLayoutMarginsRelativeArrangement) BOOL layoutMarginsRelativeArrangement;    

@end
NS_ASSUME_NONNULL_END

接下來會詳細的對這個API進行說明。


枚舉UIStackViewDistribution

該枚舉的主要的作用就是沿著堆棧軸進行布局。


/* Distribution—the layout along the stacking axis.
 
 All UIStackViewDistribution enum values fit first and last arranged subviews tightly to the container,
 and except for UIStackViewDistributionFillEqually, fit all items to intrinsicContentSize when possible.
 */
typedef NS_ENUM(NSInteger, UIStackViewDistribution) {
    
    /* When items do not fit (overflow) or fill (underflow) the space available
     adjustments occur according to compressionResistance or hugging
     priorities of items, or when that is ambiguous, according to arrangement
     order.
     */
    UIStackViewDistributionFill = 0,
    
    /* Items are all the same size.
     When space allows, this will be the size of the item with the largest
     intrinsicContentSize (along the axis of the stack).
     Overflow or underflow adjustments are distributed equally among the items.
     */
    UIStackViewDistributionFillEqually,
    
    /* Overflow or underflow adjustments are distributed among the items proportional
     to their intrinsicContentSizes.
     */
    UIStackViewDistributionFillProportionally,
    
    /* Additional underflow spacing is divided equally in the spaces between the items.
     Overflow squeezing is controlled by compressionResistance priorities followed by
     arrangement order.
     */
    UIStackViewDistributionEqualSpacing,
    
    /* Equal center-to-center spacing of the items is maintained as much
     as possible while still maintaining a minimum edge-to-edge spacing within the
     allowed area.
        Additional underflow spacing is divided equally in the spacing. Overflow 
     squeezing is distributed first according to compressionResistance priorities 
     of items, then according to subview order while maintaining the configured 
     (edge-to-edge) spacing as a minimum.
     */
    UIStackViewDistributionEqualCentering,
} NS_ENUM_AVAILABLE_IOS(9_0);

所有UIStackViewDistribution枚舉值都適用arranged subviews中第一個和最后一個緊密排列在容器中,除了UIStackViewDistributionFillEqually外,它使所有項目都使用intrinsicContentSize

下面我們就一起看一下這個枚舉值的使用。

1. UIStackViewDistributionFill

itemfitoverflow)或fillunderflow)時,根據(jù)item
compressionResistancehugging屬性,或根據(jù)排列順序的含義不明確時,可用空間調(diào)整發(fā)生。

根據(jù)stackView的大小是否固定,可以分為下面兩種情況。

  • stackView的大小是固定的

在xib中新放入一個stackView,如下所示

給stackView設(shè)置約束,距離邊距均為20。

接著看一下布局模式,默認為Fill。

這里還要說一句,設(shè)置stackView的backgroundView的背景色是不生效的,具體原因在上面一篇已經(jīng)說過了,就不多說了。

下面放入二個UIView子視圖。

先放入第一個View,設(shè)置背景色是紅色的。

這里可以看見,由于是Fill模式,就是我們拖動View1改變其大小的時候,松開鼠標還是會自動調(diào)整和stackView一樣的大小。

這里,我們放入兩個View,并放在stackView上。

我們設(shè)置藍色View的約束

可以發(fā)現(xiàn),通過調(diào)整藍色視圖的高度,紅色的視圖會自動調(diào)整填充stackview剩下的高度部分。

我們接著將藍色視圖的高度設(shè)置為400,看一下視圖變化。

可見,這種模式的變化還是很明顯的。

  • stackView的大小不是固定的

設(shè)置stackview的約束,距離左上右三部分的約束,高度不做約束。

下面我們就對紅藍視圖進行約束

藍色視圖約束

設(shè)置完后,可以看見,stackview自動調(diào)整自己高度為400,如下所示。

下面將紅色視圖的高度調(diào)整為300,看一下有什么變化。

可見,stackview可以根據(jù)他們這兩個視圖的變化,實現(xiàn)了自適應(yīng)的改變。

2. UIStackViewDistributionFillEqually

items都是相同的大小。 當空間允許時,這將是具有最大intrinsicContentSize(沿著堆棧軸)的item的大小。 上溢或下溢調(diào)整在這些items之間平均分配。

  • stackview高度有約束時

這個時候,會自動調(diào)整高度,使它們高度一致。

可以看見,設(shè)置高度約束后的stackview的高度為607。這個時候紅藍視圖就不用進行任何約束,它們在stackview中等高。

可見,它們的高度都被設(shè)置為303.5,這個是系統(tǒng)自動為我們進行計算的。

  • stackview不設(shè)置高度約束

我們設(shè)置好藍色視圖的約束,那么系統(tǒng)就會自動計算紅色視圖和stackview的約束。

3. UIStackViewDistributionFillProportionally

與item的intrinsicContentSizes成比例,進行上溢或下溢調(diào)整分配。

這個屬性,一般是stackview的高度有約束,而2個subview的高度也有約束,注意subview的高度約束的priority要低于1000。stackview可以根據(jù)subview的約束,計算出比例,進行縮放。

4. UIStackViewDistributionEqualSpacing

應(yīng)用于stackview高度確定,subview的高度也確定,想讓subview比較均勻地分布在stckview中。

額外的下溢間距在items之間的空間中等分。 上溢擠壓由arrangement
順序的compressionResistance優(yōu)先級進行控制。

5. UIStackViewDistributionEqualCentering

應(yīng)用于stackview高度確定,subview的高度也確定,想讓subview比較均勻地分布在stckview中。

item的中心到中心間距相等,盡可能保持在允許區(qū)域內(nèi)的邊緣到邊緣的最小距離。 額外的下溢間隔在間距中相等地分開。 根據(jù)items的compressionResistance優(yōu)先級分配Overflow squeezing,然后根據(jù)子視圖的順序分配Overflow squeezing,同時保持所配置的(邊緣到邊緣)間距最小。

后記

本篇主要介紹了布局的一個枚舉,感興趣的給個贊或者關(guān)注~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372

推薦閱讀更多精彩內(nèi)容