標籤:
Autoresizing:出現最早,僅僅能夠針對父控制項做約束(注意:要關閉Autolayout&Size classes才能夠看到Autoresizing)
代碼對應:
UIView.h中的autoresizingMask屬性
@property(nonatomic) UIViewAutoresizing autoresizingMask; // simple resize. default is UIViewAutoresizingNone
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
Autolayout:不僅僅能夠針對父控制項約束,而且可以針對同級進行約束,但對不同裝置的橫豎屏還是有依賴
Size classes:將所有尺寸分為Regular(標準的)compact(緊湊的) any(任意的)三種情況 進行組合,不再依賴於死的尺寸,這就有效解決了Autolayout的弊端
Size classes:
但是我們看到圖中的寬度和高度都是Any,Any是什麼意思呢?如果weight設為Any,height設定為Regular,那麼在該狀態下的介面元素在只要height為Regular,無論weight是Regular還是Compact的狀態中都會存在。這種關係應該叫做繼承關係,具體的四種介面描述與可繼承的介面描述如下:
- w:Compact h:Compact 繼承 (w:Any h:Compact , w:Compact h:Any , w:Any h:Any)
- w:Regular h:Compact 繼承 (w:Any h:Compact , w:Regular h:Any , w:Any h:Any)
- w:Compact h:Regular 繼承 (w:Any h:Regular , w:Compact h:Any , w:Any h:Any)
- w:Regular h:Regular 繼承 (w:Any h:Regular , w:Regular h:Any , w:Any h:Any)
我們知道了iOS 8下面裝置介面可以描述為4種,但是這麼多裝置(iPhone4S,iPhone5/5s,iPhone6,iPhone6 Plus,iPad,Apple Watch)具體對應什麼描述呢?經過查看官方文檔和具體實踐得知具體對應關係如下:
- iPhone4S,iPhone5/5s,iPhone6
- 豎屏:(w:Compact h:Regular)
- 橫屏:(w:Compact h:Compact)
- iPhone6 Plus
- 豎屏:(w:Compact h:Regular)
- 橫屏:(w:Regular h:Compact)
- iPad
- 豎屏:(w:Regular h:Regular)
- 橫屏:(w:Regular h:Regular)
- Apple Watch(猜測)
- 豎屏:(w:Compact h:Compact)
- 橫屏:(w:Compact h:Compact)
代碼對應:UITraitCollection
UIViewController.h
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
UITraitCollection.h(普通View)
/*! Trait environments expose a trait collection that describes their environment. */
@protocol UITraitEnvironment <NSObject>
@property (nonatomic, readonly) UITraitCollection *traitCollection;
/*! To be overridden as needed to provide custom behavior when the environment‘s traits change. */
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection;
@end
Autolayout:
代碼對應:
NSLayoutConstraint
注意:手碼自動布局先關閉UIView的以下屬性
1.- (BOOL)translatesAutoresizingMaskIntoConstraints NS_AVAILABLE_IOS(6_0); // Default YES
2.1VFL語言
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
2.2純粹代碼
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
詳情:http://www.cnblogs.com/zhw511006/p/3998534.html
iOS Autoresizing Autolayout Size classes