iOS Masonry learn issue.

2016-09-13 0 条评论 259 次阅读 0 人点赞

```// // FranchiseeVIewController.m // NewBoBo // // Created by TestSmirk on 16/9/13. // Copyright © 2016年 bzby. All rights reserved. //

import "FranchiseeVIewController.h"

import "Masonry.h"

@interface FranchiseeVIewController () @property (nonatomic,strong)UILabel <em>label; @property (nonatomic,strong)UILabel labelLeft; @property (nonatomic,strong)UILabel rightLabel; @end @implementation FranchiseeVIewController

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [self createLeftItemWithImage:[UIImage imageNamed:@"navigationBackImage"]]; [self setNavigateionTitle:@"业务管理"]; [self initView]; } // tell UIKit that you are using AutoLayout
  • (BOOL)requiresConstraintBasedLayout { return YES; } -(void)updateViewConstraints{ [_label mas_remakeConstraints:^(MASConstraintMaker make) { make.width.equalTo(@(self.view.width)); make.height.equalTo(@(44)); make.topMargin.equalTo(@(22)); }]; [_labelLeft mas_remakeConstraints:^(MASConstraintMaker make) { make.width.equalTo(@(self.view.width/2)); make.height.equalTo(@(22)); make.top.equalTo(@(_label.bottom)); }]; [_rightLabel mas_remakeConstraints:^(MASConstraintMaker make) { make.width.width.equalTo(@(self.view.width/2)); make.height.equalTo(@(22)); make.top.equalTo(@(_label.bottom)); make.left.equalTo(@(_labelLeft.right)); }]; [super updateViewConstraints]; } -(void)initView{ _label = [[UILabel alloc]init]; _label.text = @"123"; _label.backgroundColor = [UIColor blueColor]; [self.view addSubview:_label]; _labelLeft = [[UILabel alloc]init]; _labelLeft.backgroundColor = [UIColor purpleColor]; _labelLeft.text = @"11"; [self.view addSubview:_labelLeft]; _rightLabel = [[UILabel alloc]init]; _rightLabel.backgroundColor = [UIColor orangeColor]; _rightLabel.text = @"11111"; [self.view addSubview:_rightLabel]; } -(void)leftItemClickedHandle:(UIBarButtonItem )sender{ [self.navigationController popViewControllerAnimated:YES]; }
  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /</em>

    pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } / @end

<pre class="line-numbers prism-highlight" data-start="1"><code class="language-null">后来改成了: </code></pre>

// // FranchiseeVIewController.m // NewBoBo // // Created by TestSmirk on 16/9/13. // Copyright © 2016年 bzby. All rights reserved. //

import "FranchiseeVIewController.h"

import "Masonry.h"

@interface FranchiseeVIewController () @property (nonatomic,strong)UILabel <em>label; @property (nonatomic,strong)UILabel labelLeft; @property (nonatomic,strong)UILabel rightLabel; @end @implementation FranchiseeVIewController

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [self createLeftItemWithImage:[UIImage imageNamed:@"navigationBackImage"]]; [self setNavigateionTitle:@"业务管理"]; [self initView]; } // tell UIKit that you are using AutoLayout
  • (BOOL)requiresConstraintBasedLayout { return YES; } -(void)updateViewConstraints{ UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10); [_label mas_remakeConstraints:^(MASConstraintMaker make) { make.width.equalTo(@(self.view.frame.size.width)); make.height.equalTo(@(44)).with.insets(padding); make.left.equalTo(self.view); make.topMargin.equalTo(@(22)); }]; [_labelLeft mas_remakeConstraints:^(MASConstraintMaker make) { make.top.equalTo(_label.mas_bottom).offset(0); make.size.mas_equalTo(CGSizeMake(SCREEN_WIDHT/2, 22)); }]; [_rightLabel mas_remakeConstraints:^(MASConstraintMaker make) { make.width.width.equalTo(@(self.view.width/2)); make.height.equalTo(@(22)); make.top.equalTo(_label.mas_bottom); make.left.equalTo(_labelLeft.mas_right); }]; [super updateViewConstraints]; } -(void)initView{ _label = [[UILabel alloc]init]; _label.text = @"123"; _label.backgroundColor = [UIColor blueColor]; [self.view addSubview:_label]; _labelLeft = [[UILabel alloc]init]; _labelLeft.backgroundColor = [UIColor purpleColor]; _labelLeft.text = @"11"; [self.view addSubview:_labelLeft]; _rightLabel = [[UILabel alloc]init]; _rightLabel.backgroundColor = [UIColor orangeColor]; _rightLabel.text = @"11111"; [self.view addSubview:_rightLabel]; } -(void)leftItemClickedHandle:(UIBarButtonItem )sender{ [self.navigationController popViewControllerAnimated:YES]; }
  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /</em>

    pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } / @end
    
    总结:
  • 获取view尺寸要用mas_加前缀.不加mas_打开的时候会有计算延时.

TestSmirk

这个人太懒什么东西都没留下

文章评论(0)