コードによる自動レイアウト

5303 ワード

AutoresizingMaskメソッドについて
簡単なインタフェースの適切な時にautoresizingMaskとその便利さを使うのはシステムの方法なので直接その属性を設定すればいいです
まず、UIViewAutoresizingは列挙タイプであり、デフォルトはUIViewAutoresizingNoneであり、つまり何も処理されていないことを知っておく必要があります.
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone                 = 0,//            
UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,//    view       ,        
UIViewAutoresizingFlexibleWidth        = 1 << 1,//    view   ,           
UIViewAutoresizingFlexibleRightMargin  = 1 << 2,//    view       ,        
UIViewAutoresizingFlexibleTopMargin    = 1 << 3,//    view       ,        
UIViewAutoresizingFlexibleHeight       = 1 << 4,//    view   ,            
UIViewAutoresizingFlexibleBottomMargin = 1 << 5//    view        ,        
};

Autoresizingの組み合わせ:
すなわち、列挙中の値は|で区切ることができ、同時に複数の値を持つ機能があり、異なるシーンに対して異なる変化を行うことができる.例:
//   View                 ,       、            
[View setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];

AutolayoutサードパーティフレームワークMasonry
複雑なインターフェースであればいろいろな依存関係があるので持ち運び方法が足りないので、サードパーティフレームワークのMasonry(使う人が多い)をお勧めします
インストール:1.Masonryフォルダのすべてのソースコードをプロジェクトに追加2つのマクロを追加し、メインヘッダファイルをインポート
 //         ,    mas_  
#define MAS_SHORTHAND
//        ,equalTo    mas_equalTo
#define MAS_SHORTHAND_GLOBALS
//                  
#import "Masonry.h"

2.pod'Masonry'を使う
方法
コンストレイントを追加する方法
mas_make //             
 -> [ mas_makeConstraints:^(MASConstraintMaker *make){}];

mas_update //                   
 -> [ mas_updateConstraints:^(MASConstraintMaker *make){}];

mas_remake //                   
 -> [ mas_remakeConstraints:^(MASConstraintMaker *make){}];

Demo:
**中央に**を表示
UIView *redView = [[UIView alloc] init]; 
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView]; 
 //              。
 [redView mas_makeConstraints:^(MASConstraintMaker *make) { 
  make.centerX.equalTo(self.view);
  make.centerY.equalTo(self.view);
  make.height.equalTo(100);
  make.width.equalTo(200);
 }];

下に並べて、間隔20
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

UIView *blueView= [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];

//     
[redView makeConstraints:^(MASConstraintMaker *make) {
   make.left.equalTo(self.view.left).offset(20); //     20
   make.right.equalTo(blueView.left).offset(-20); //    blueView  20
   make.bottom.equalTo(self.view.bottom).offset(-20); //     20

   make.height.equalTo(200); //   200

}];

  [blueView makeConstraints:^(MASConstraintMaker *make) {
   make.right.equalTo(self.view.right).offset(-20); //     20
   make.bottom.equalTo(redView.bottom); //  redView      

   make.height.equalTo(redView); //   
   make.width.equalTo(redView); //   
}];

4つのビューをスクリーン全体に分割
  //  
  UIView *redView = [[UIView alloc] init];
  redView.backgroundColor = [UIColor redColor];
  [self.view addSubview:redView];
  //   
  UIView *blueView= [[UIView alloc] init];
  blueView.backgroundColor = [UIColor blueColor];
  [self.view addSubview:blueView];
 //   
 UIView *blackView = [[UIView alloc] init];
 blackView.backgroundColor = [UIColor blackColor];
 [self.view addSubview:blackView];
 //   
 UIView *greenView= [[UIView alloc] init];
 greenView.backgroundColor = [UIColor greenColor];
 [self.view addSubview:greenView];


 // autolayout
 [redView makeConstraints:^(MASConstraintMaker *make) {
     make.left.and.top.equalTo(self.view);
     make.right.equalTo(self.view.centerX);
     make.bottom.equalTo(self.view.centerY);
 }];

 [blueView makeConstraints:^(MASConstraintMaker *make) {
     make.left.equalTo(redView.right);
     make.right.equalTo(self.view);
     make.height.equalTo(redView);

 }];

 [blackView makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(redView.bottom);
     make.height.equalTo(redView);
     make.width.equalTo(redView);
 }];

 [greenView makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(blueView.bottom);
     make.left.equalTo(blackView.right);
     make.height.equalTo(blueView);
     make.width.equalTo(blueView);
 }];

 //   View  
 UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"010.png"]];
 UILabel *name = [[UILabel alloc] init];
 name.text = @"  ";
 name.textAlignment = NSTextAlignmentCenter;
 name.backgroundColor = [UIColor purpleColor];
 [redView addSubview:image];
 [redView addSubview:name];
 [image makeConstraints:^(MASConstraintMaker *make) {
     make.center.equalTo(redView.center).offset(-20);
 }];
 [name makeConstraints:^(MASConstraintMaker *make) {
     make.left.right.equalTo(redView.left);
     make.bottom.equalTo(redView.bottom);
     make.height.equalTo(40);
 }];

参考自動レイアウトのautoresizingMask使用詳細(Storyboard&Code)Masonry ios開発学習ノート040-autolayoutサードパーティフレームワークMasonry