在项目中使用Masonry实现自动布局Autolayout(1)

来源:互联网 发布:python 没有cv2.cv 编辑:程序博客网 时间:2024/05/23 13:38

       最近都在研究Autolayout的内容,随着iPhone手机屏幕尺寸的增加,屏幕适配变得尤为重要;适配有多种方式,当然也是可以根据相对位置来适配,但是计算尺寸和代码量可能有些大(如果不嫌麻烦的话);而另外一种就是Autolayout,网上有很多直接拖拽控件来适配界面的教程,但是自己习惯用纯代码写UI,所以还是用纯代码写。起初,看Apple的Autolayout,只能说头都大,几个简单的UI约束就要很多代码,看起来也费尽。这里有篇文章,感兴趣的可以参考了解一下:http://chun.tips/blog/2014/10/27/wei-iphone6she-ji-zi-gua-ying-bu-ju-(chun-dai-ma-shi-xian-)/。

      Masonry,一个轻量级的布局框架,同时支持iOS和Mac OS X,采用DSL链式语法,只能说,这个框架很好用,github地址:Masonry 。

      这个框架有比较多的属性,这里不一一列举了,比较常用的属性有,这些属性与NSLayoutAttrubute的对照表如下:

  

      github主页上也有相关的说明、例子等。我要做成的效果如下:

    

      具体的布局思路如下:

  

      下面是主要代码的实现部分:

[objc] view plaincopy
  1. //表头视图  
  2. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  3. {  
  4.     HeaderView = [[UIView alloc] initWithFrame:CGRectMake(00, kDeviceWidth, 2*(kDeviceHeight/9))];  
  5.     HeaderView.backgroundColor = NAVIGATION_COLOR;  
  6.       
  7.     topView = [UIView new];  
  8.     topView.backgroundColor = [UIColor clearColor];  
  9.     [HeaderView addSubview:topView];  
  10.     [topView mas_makeConstraints:^(MASConstraintMaker *make) {  
  11.         make.top.left.and.right.equalTo(HeaderView);  
  12.     }];  
  13.       
  14.     UIView *buttomView = [UIView new];  
  15.     buttomView.backgroundColor = [UIColor clearColor];  
  16.     [HeaderView addSubview:buttomView];  
  17.     [buttomView mas_makeConstraints:^(MASConstraintMaker *make) {  
  18.         make.left.right.and.bottom.equalTo(HeaderView);  
  19.         make.top.equalTo(topView.mas_bottom);  
  20.         make.width.and.height.equalTo(topView);  
  21.     }];  
  22.       
  23.     headerImageV = [[UIImageView alloc] init];  
  24.     headerImageV.clipsToBounds = YES;  
  25.     headerImageV.layer.cornerRadius = (HeaderView.frame.size.height/2) / 2.0f;  
  26.     headerImageV.image = [UIImage imageNamed:@"store.jpg"];  
  27.     [HeaderView addSubview:headerImageV];  
  28.     [headerImageV mas_makeConstraints:^(MASConstraintMaker *make) {  
  29.         make.width.equalTo(headerImageV.mas_height).multipliedBy(1);  
  30.           
  31.         make.width.and.height.lessThanOrEqualTo(topView);  
  32.         make.width.and.height.equalTo(topView).with.priorityLow();  
  33.           
  34.         make.centerX.equalTo(topView);  
  35.         if (didRotato == NO) {  
  36.             make.bottom.equalTo(topView.mas_bottom).offset(20);  
  37.         }  
  38.         else  
  39.         {  
  40.             make.top.equalTo(topView.mas_top);  
  41.         }  
  42.     }];  
  43.       
  44.     UILabel *shopNameL = [[UILabel alloc] init];  
  45.     shopNameL.textColor = [UIColor whiteColor];  
  46.     shopNameL.font = [UIFont systemFontOfSize:20];  
  47.     shopNameL.textAlignment = NSTextAlignmentCenter;  
  48.     shopNameL.text = @"精诚超市";  
  49.     [HeaderView addSubview:shopNameL];  
  50.     [shopNameL mas_makeConstraints:^(MASConstraintMaker *make) {  
  51.         make.top.equalTo(headerImageV.mas_bottom);  
  52.         make.left.and.right.equalTo(buttomView);  
  53.     }];  
  54.       
  55.     UILabel *addressL = [[UILabel alloc] init];  
  56.     addressL.font = [UIFont systemFontOfSize:10];  
  57.     addressL.textAlignment = NSTextAlignmentRight;  
  58.     addressL.textColor = [UIColor whiteColor];  
  59.     addressL.text = @"天河区万佳广场首层234号";  
  60.     [HeaderView addSubview:addressL];  
  61.       
  62.     UILabel *gwNumberL = [[UILabel alloc] init];  
  63.     gwNumberL.font = addressL.font;  
  64.     gwNumberL.textColor = addressL.textColor;  
  65.     gwNumberL.textAlignment = NSTextAlignmentLeft;  
  66.     gwNumberL.text = @"  GW88888888";  
  67.     [HeaderView addSubview:gwNumberL];  
  68.       
  69.     [addressL mas_makeConstraints:^(MASConstraintMaker *make) {  
  70.         make.top.equalTo(shopNameL.mas_bottom);  
  71.         make.left.equalTo(buttomView.mas_left);  
  72.         make.right.equalTo(gwNumberL.mas_left);  
  73.         make.height.equalTo(shopNameL.mas_height);  
  74.         make.bottom.equalTo(buttomView.mas_bottom);  
  75.     }];  
  76.       
  77.     [gwNumberL mas_makeConstraints:^(MASConstraintMaker *make) {  
  78.         make.top.equalTo(shopNameL.mas_bottom);  
  79.         make.left.equalTo(addressL.mas_right);  
  80.         make.right.equalTo(buttomView.mas_right);  
  81.         make.width.equalTo(addressL.mas_width);  
  82.         make.height.equalTo(addressL.mas_height);  
  83.         make.bottom.equalTo(buttomView.mas_bottom);  
  84.     }];  
  85.       
  86.     return HeaderView;  
  87. }  
      cell中主要的布局代码如下:

[objc] view plaincopy
  1. - (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  2. {  
  3.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  4.     if (self) {  
  5.         self.selectionStyle = UITableViewCellSelectionStyleNone;  
  6.           
  7.         _theImageV = [[UIImageView alloc] init];  
  8.         _theImageV.autoresizingMask = UIViewAutoresizingFlexibleHeight;  
  9.         [self.contentView addSubview:_theImageV];  
  10.           
  11.         _nameL = [[UILabel alloc] init];  
  12.         _nameL.font = [UIFont systemFontOfSize:18];  
  13.         [self.contentView addSubview:_nameL];  
  14.           
  15.         UIView *topView = [UIView new];  
  16.         topView.backgroundColor = [UIColor clearColor];  
  17.         [self.contentView addSubview:topView];  
  18.           
  19.         /* 约束关系 */  
  20.         [topView mas_makeConstraints:^(MASConstraintMaker *make) {  
  21.             make.centerY.equalTo(self.contentView);  
  22.             make.width.and.height.equalTo(self.contentView.mas_height);  
  23.         }];  
  24.           
  25.         [_theImageV mas_makeConstraints:^(MASConstraintMaker *make) {  
  26.             make.edges.equalTo(topView).insets(UIEdgeInsetsMake(12121212)); //各边偏移12个点  
  27.             make.center.equalTo(topView);  
  28.         }];  
  29.           
  30.         [_nameL mas_makeConstraints:^(MASConstraintMaker *make) {  
  31.             make.centerY.equalTo(self.contentView);  
  32.             make.left.equalTo(_theImageV.mas_right).insets(kPadding);  
  33.         }];  
  34.           
  35.           
  36.     }  
  37.     return self;  
  38. }  
       你可以从github上获取到完整代码,地址:this
0 0
原创粉丝点击