定義:Springs&Struts決定了當UIView的父View的bounds變化時,其自身的座標如何變化:是有flexible or fixed margins (the struts), width and height (the springs)。UIView的autoresizingMask屬性有如下列舉值:UIViewAutoresizingFlexibleLeftMargin、UIViewAutoresizingFlexibleWidth、UIViewAutoresizingFlexibleRightMargin、UIViewAutoresizingFlexibleTopMargin、UIViewAutoresizingFlexibleHeight和UIViewAutoresizingFlexibleBottomMargin(這些值可以取或進行合併)。
1.Designing by intent。 Autolayout最大的優點就是擺脫以前手寫frame的煩惱,用constraints來描述view本身或者view和view之間的佈局關係。這是完全不同的思維也即是Designing by intent。一旦constraints建立好,剩下的都由Autolayout來替我們解決。
3.在view多的時候或者對效能要求比較高的場合下效能會出現問題。這才是autolayout在應用到實踐過程中最大的問題。這兩篇文章定量的評測了autolayout在不同數量view下的效能表現Auto Layout Performance on iOS 和Optimising Autolayout
。而在實際應用過程中,的確也會有效能問題。搜狗iOS輸入法在最初適配不同鍵盤尺寸的按鍵佈局時,使用了 Autolayout 的解決方案,影響了鍵盤調起速度,後續針對鍵盤調起速度的優化過程中,通過Instruments的Time Profiler才定位到該問題,參考搜狗輸入法效能優化實踐。還有XING app的開發者在他們在專案初期,在列表的佈局上使用了Autolayout。帶來了無法解決的效能問題,所以放棄使用Autolayout,部落格連結Auto Layout in Cells – Don’t!。還有LinkedIn團隊也遇到了同樣的問題,他們為此開發了LayoutKit來代替Autolayout,他們在介紹LayoutKit的時候,這樣寫道"LinkedIn created LayoutKit because we have found that Auto Layout is not performant enough for complicated view hierarchies in scrollable views."
6.Stack Views. UIStackView(iOS9+)class provides a streamlined interface for laying out a collection of views in either a column or a row. Stack views let you leverage the power of Auto Layout, creating user interfaces that can dynamically adapt to the device’s orientation, screen size, and any changes in the available space. The stack view manages the layout of all the views in its arrangedSubviews property. These views are arranged along the stack view’s axis, based on their order in the arrangedSubviews array. The exact layout varies depending on the stack view’s axis, distribution, alignment, spacing, and other properties. 其實Stack View和Android的Linear Layout類似,都是提供在同一方向(橫向或者縱向)佈局多個view的便捷方法。我覺得iOS中不僅僅是UIStackView,UICollectionView使用得當,在特定的應用場景下也可以簡單地實現適配不同螢幕的作用。
1."ComponentKit takes a functional, declarative approach to building UI and emphasizes a one-way data flow from immutable models to immutable components that describe how views should be configured" "It was built to power Facebook's News Feed and is now used throughout the Facebook iOS app."
2."It performs layout on a background thread, creates the minimal view hierarchy to render the components on screen and has intelligent view reuse. This results in great scroll performance and a snappy system overall."
LayoutKit 官方介紹"LinkedIn created LayoutKit because we have found that Auto Layout is not performant enough for complicated view hierarchies in scrollable views."並且和autolayout做了對比:
LayoutKit has many benefits over using Auto Layout:
1.Fast: LayoutKit is as fast as manual layout code and significantly faster than Auto Layout.
2.Asynchronous: Layouts can be computed in a background thread so user interactions are not interrupted.
3.Declarative: Layouts are declared with immutable data structures. This makes layout code easier to develop, code review, debug, and maintain.
4.Cacheable: Layout results are immutable data structures so they can be precomputed in the background and cached to increase user perceived performance.
Relative Layout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).
Linear Layout(和iOS中的UIStackView類似)LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.
Building Layouts with an Adapter, 比如List View(UITableView)和Grid View(UICollectionView)
Constraint Layout(Autolayout) ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are layed out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.
The foundation of Android's support for multiple screens is its ability to manage the rendering of an application's layout and bitmap drawables in an appropriate way for the current screen configuration. The system handles most of the work to render your application properly on each screen configuration by scaling layouts to fit the screen size/density and scaling bitmap drawables for the screen density, as appropriate. To more gracefully handle different screen configurations, however, you should also:
Explicitly declare in the manifest which screen sizes your application supports
Provide different bitmap drawables for different screen densities
Using configuration qualifiers(Size、Density、Orientation和Aspect ratio等)
Designing alternative layouts and drawables
Best Practices
Use wrap_content, match_parent, or the dp(iOS中的point類似,) unit for layout dimensions. If you use "wrap_content"(iOS中的Intrinsic Content Size類似), the width or height of the view is set to the minimum size necessary to fit the content within that view, while "match_parent" makes the component expand to match the size of its parent view.
Use RelativeLayout. Do not use hard-coded pixel values in your application code.Do not use AbsoluteLayout
Use Size Qualifiers. Your application should also provide several alternative layouts to target different screen configurations. You do so by using configuration qualifiers, which allows the runtime to automatically select the appropriate resource based on the current device’s configuration (such as a different layout design for different screen sizes).
Use the Smallest-width Qualifier. The Smallest-width qualifier allows you to target screens that have a certain minimum width given in dp.
Use Orientation Qualifiers.
Use Layout Aliases 為了相容性和減少重複佈局,Layout Aliases可以是多個限定符描述對應同一個layout.xml
Use Nine-patch Bitmaps(.9.png), 和iOS中的resizable image類似,Xcode中的Image Slicing也可以幫助開發者用視覺化的方式完成resizable image
其中有幾個選項大家可以關注下,一個是Width還有Height。這個就是可以指定Size Class的選項。還有Scale Factors,這個選項有Single Vector或Vector With Overrides(Vector With Overrides是Single Vector的增強, 可以在放置完向量圖之後繼續放置@1x、@2x和@3x的png格式的圖片。放置的png會優先覆蓋向量圖, 未放置對應倍率圖片的裝置才會使用向量圖對應生成的圖片),這個選項主要是為了支援向量圖,可以只放一張PDF的圖片,編譯後Xcode會自動生成1x,2x,3x的圖,需要注意的是使用他並不會減少包的體積,因為不是執行時的特性。還有Slicing,可以在圖形化介面裡設定拉伸引數。雖然Image Asset有諸如使用方便等優點,但是我們在使用過程中還是需要衡量其帶來的便捷性和問題。(1)比如Image Asset會生成1X圖片,目前很多應用已經不支援1X的裝置了,這樣會帶來包大小的問題。(2)Image Asset雖然支援向量圖,但是並不是執行時的特性,編譯後仍然會生成1x 2x 3x圖片,對安裝包大小沒有起到減少的作用,甚至會更大(一個是多了1x圖片,另外一個是如果自己提供2x 3x圖片可在新增前用一些工具用png圖片壓縮),在圖片對安裝包大小影響的優化上,一個優化點是可以自己實現對向量圖的支援,這樣就提供一份PDF即可,另一個是使用工具對png圖片壓縮,還一個比較極端優化的方式是自己在程式碼裡面畫圖片(可以第一次畫然後本地化,之後就直接讀取磁碟檔案),在具體實施的過程中,可以借用PaintCode來節省自己編寫繪圖程式碼的時間。 (3)還有其Image Asset只支援imageNamed,這樣如果一些應用想有自己的圖片快取策略的話就無法實現了。
3.UITraitCollection。 Apple的API文件裡面這樣介紹UITraitCollection: A trait collection describes the iOS interface environment for your app, including traits such as horizontal and vertical size class, display scale, and user interface idiom. To create an adaptive interface, write code to adjust your app’s layout according to changes in these traits.
The iOS trait environment is exposed though the traitCollection property of the UITraitEnvironment protocol. This protocol is adopted by the following classes: UIScreen, UIWindow, UIViewController, UIPresentationController, and UIView. You access specific trait values using the UITraitCollection horizontalSizeClass, verticalSizeClass, displayScale, and userInterfaceIdiom properties. The values that express idiom and size traits are defined in the UIUserInterfaceIdiom and UIUserInterfaceSizeClass enumerations; the value for the display scale trait is expressed as a floating point number.
To make your view controllers and views responsive to changes in the iOS interface environment, override the traitCollectionDidChange: method from the trait environment protocol. To customize view controller animations in response to interface environment changes, override the willTransitionToTraitCollection:withTransitionCoordinator: method of the UIContentContainer protocol.
5.Layout Guides參考 The UILayoutGuide class defines a rectangular area that can interact with Auto Layout. Use layout guides to replace the dummy views you may have created to represent inter-view spaces or encapsulation in your user interface. Traditionally, there were a number of Auto Layout techniques that required dummy views. A dummy view is an empty view that does not have any visual elements of its own and serves only to define a rectangular region in the view hierarchy. For example, if you wanted to use constraints to define the size or location of an empty space between views, you needed to use a dummy view to represent that space. If you wanted to center a group of objects, you needed a dummy view to contain those objects. Similarly, dummy views could be used to contain and encapsulate part of your user interface. Dummy views let you break up a large, complex user interface into self-contained, modular chunks. When used properly, they could greatly simplify your Auto Layout constraint logic.
There are a number of costs associated with adding dummy views to your view hierarchy. First, there is the cost of creating and maintaining the view itself. Second, the dummy view is a full member of the view hierarchy, which means that it adds overhead to every task the hierarchy performs. Worst of all, the invisible dummy view can intercept messages that are intended for other views, causing problems that are very difficult to find.
The UILayoutGuide class is designed to perform all the tasks previously performed by dummy views, but to do it in a safer, more efficient manner. Layout guides do not define a new view. They do not participate in the view hierarchy. Instead, they simply define a rectangular region in their owning view’s coordinate system that can interact with Auto Layout.
Layout guides can be used to define an equal spacing between a series of views.
Layout guides can also act as a black box, containing a number of other views and controls. This lets you encapsulate part of your view, breaking your layout into modular chunks.