iOS Autoresizing Autolayout Size classes

來源:互聯網
上載者:User

標籤:

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設為Anyheight設定為Regular,那麼在該狀態下的介面元素在只要heightRegular,無論weightRegular還是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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.