码迷,mamicode.com
首页 > 移动开发 > 详细

iOS masonry动态约束控件位置

时间:2015-07-01 12:25:27      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ios   masonry   


#import "FourViewController.h"
#import "View+MASAdditions.h"
#import "FiveViewControllerr.h"
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
static NSString const*kStoryboardName=@"FourViewController";
static NSString const*kIdentifier=@"FourViewController";

@interface FourViewController ()
@property (strong, nonatomic)  UIButton *move;
@property (strong, nonatomic)  UIButton *back;
@property (strong, nonatomic)  UIImageView *imageView;
@property (assign, nonatomic) int s;
@end

@implementation FourViewController
+ (instancetype)createViewControllerWithStoryboard:(id)createArgs{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:(NSString *)kStoryboardName bundle:nil];
    FourViewController *vc=[storyboard instantiateViewControllerWithIdentifier:(NSString *)kIdentifier];
    return vc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initRightItem];
    _s=10;
    [self initFirstContraints];
}
- (void)initFirstContraints{
    //WS(ws);
//    UIView *view=[UIView new];
//    view.backgroundColor=[UIColor brownColor];
//    [self.view addSubview:view];
//    [view mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view).with.offset(0);
//        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height));
//    }];
    //int width=([UIScreen mainScreen].bounds.size.width-_s*2-10)/2;
    
    _move=[[UIButton alloc]init];
    _move.backgroundColor=[UIColor blueColor];
    [self.view addSubview:_move];
    [_move mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(_s);
        make.top.equalTo(self.view).with.offset(100);
        make.width.equalTo(@150);
        make.height.equalTo(@40);
    }];
    [_move addTarget:self action:@selector(moveFrame) forControlEvents:UIControlEventTouchUpInside];
    [_move setTitle:@"移动图片" forState:UIControlStateNormal];
    
    _back=[[UIButton alloc]init];
    _back.backgroundColor=[UIColor greenColor];
    [self.view addSubview:_back];
    [_back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).with.offset(-_s);
        make.top.equalTo(self.view).with.offset(100);
        make.width.equalTo(@150);
        make.height.equalTo(@40);
    }];
    [_back addTarget:self action:@selector(backFrame) forControlEvents:UIControlEventTouchUpInside];
    [_back setTitle:@"图片回来" forState:UIControlStateNormal];
    
    _imageView=[[UIImageView alloc]init];
    _imageView.image=[UIImage imageNamed:@"move"];
    [self.view addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(_s);
        make.top.equalTo(self.view).with.offset(200);
        make.width.equalTo(@150);
        make.height.equalTo(@150);
    }];
}
- (void)moveFrame{
    _s=_s+5;;
    NSLog(@"_s:%d",_s);
    [_imageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(_s);
        make.top.equalTo(self.view).with.offset(200+_s);
        make.width.equalTo(@150);
        make.height.equalTo(@150);
    }];
}
- (void)backFrame{
    _s=_s-5;;
    NSLog(@"_s:%d",_s);
    [_imageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(_s);
        make.top.equalTo(self.view).with.offset(200-_s);
        make.width.equalTo(@150);
        make.height.equalTo(@150);
    }];
}
- (void)initRightItem{
    [self setTitle:@"动态改变控件位置"];
    UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithTitle:@"多约束" style:UIBarButtonItemStylePlain target:self action:@selector(right)];
    self.navigationItem.rightBarButtonItem=right;
}
- (void)right{
    FiveViewControllerr *vc=[FiveViewControllerr createViewControllerWithStoryboard:nil];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS masonry动态约束控件位置

标签:ios   masonry   

原文地址:http://blog.csdn.net/darongzi1314/article/details/46707173

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!