欢迎使用CSDN-markdown编辑器

来源:互联网 发布:能挣钱的软件 编辑:程序博客网 时间:2024/05/24 02:36

使用masonry进行屏幕适配

masonry是第一个第三方autolayout库,个人觉得比较好用。
下载链接:https://github.com/SnapKit/Masonry


简单的使用

_editImageView = [[UIImageView alloc] init];        [self addSubview:_editImageView];        _editImageView.backgroundColor = [UIColor redColor];        _editButton = [[UIButton alloc] init];        [_editButton setTitle:@"编辑" forState:UIControlStateNormal];        _editButton.backgroundColor = [UIColor redColor];        [self addSubview:_editButton];        //        _downloadImageView = [[UIImageView alloc] init];        [self addSubview:_downloadImageView];        _downloadImageView.backgroundColor = [UIColor yellowColor];        _downloadButton = [[UIButton alloc] init];        [_downloadButton setTitle:@"下载" forState:UIControlStateNormal];        _downloadButton.backgroundColor = [UIColor yellowColor];        [self addSubview:_downloadButton];        //        _deleteImageView = [[UIImageView alloc] init];        [self addSubview:_deleteImageView];        _deleteImageView.backgroundColor = [UIColor blueColor];        _deleteButton = [[UIButton alloc] init];        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];        _deleteButton.backgroundColor = [UIColor blueColor];        [self addSubview:_deleteButton];        _shareImageView = [[UIImageView alloc] init];        [self addSubview:_shareImageView];        _shareImageView.backgroundColor = [UIColor greenColor];        _weixinButton = [[UIButton alloc] init];        _weixinButton.backgroundColor = [UIColor greenColor];        [_weixinButton setTitle:@"微信" forState:UIControlStateNormal];        [self addSubview:_weixinButton];        _friendsCircleButton = [[UIButton alloc] init];        _friendsCircleButton.backgroundColor = [UIColor greenColor];        [_friendsCircleButton setTitle:@"朋友圈" forState:UIControlStateNormal];        [self addSubview:_friendsCircleButton];        _weiboButton = [[UIButton alloc] init];        _weiboButton.backgroundColor = [UIColor greenColor];        [_weiboButton setTitle:@"微博" forState:UIControlStateNormal];        [self addSubview:_weiboButton];        _qqButton = [[UIButton alloc] init];        _qqButton.backgroundColor = [UIColor greenColor];        [_qqButton setTitle:@"qq" forState:UIControlStateNormal];        [self addSubview:_qqButton];        _addNewImageView = [[UIImageView alloc] init];        [self addSubview:_addNewImageView];        _addNewImageView.backgroundColor = [UIColor purpleColor];        _addNewButton = [[UIButton alloc] init];        _addNewButton.backgroundColor = [UIColor purpleColor];        [_addNewButton setTitle:@"增加" forState:UIControlStateNormal];        [self addSubview:_addNewButton];        //设置autolayout        [_editImageView makeConstraints:^(MASConstraintMaker *make) {            make.left.equalTo(self.left).offset(10);            make.right.equalTo(_editButton.left).offset(-2);            make.top.equalTo(self.top).offset(120);            make.bottom.equalTo(_downloadImageView.mas_top).offset(-10);            make.width.equalTo(_downloadImageView.width);            make.width.equalTo(_editImageView.width);            make.width.equalTo(_shareImageView.width);            make.width.equalTo(_addNewImageView.width);            make.height.equalTo(_downloadImageView.height);            make.height.equalTo(_editImageView.height);            make.height.equalTo(_shareImageView.height);            make.height.equalTo(_addNewImageView.height);        }];        [_editButton makeConstraints:^(MASConstraintMaker *make) {            make.right.equalTo(self.right).offset(-100);            make.width.equalTo(_addNewImageView.mas_width).offset(60);            make.height.and.bottom.and.top.equalTo(_editImageView);        }];        [_downloadImageView makeConstraints:^(MASConstraintMaker *make) {            make.left.and.right.equalTo(_editImageView);            make.bottom.equalTo(_deleteImageView.top).offset(-10);            make.width.equalTo(@[_editImageView.width,_deleteImageView.width,_shareImageView.width,_addNewImageView.width]);            make.height.equalTo(@[_editImageView.height,_deleteImageView.height,_shareImageView.height,_addNewImageView.height]);        }];        [_downloadButton makeConstraints:^(MASConstraintMaker *make) {            make.right.equalTo(self.right).offset(-100);            make.width.equalTo(_addNewImageView.mas_width).offset(60);            make.height.and.bottom.and.top.equalTo(_downloadImageView);        }];        [_deleteImageView makeConstraints:^(MASConstraintMaker *make) {            make.left.and.right.equalTo(_editImageView);            make.bottom.equalTo(_shareImageView.top).offset(-10);            make.width.equalTo(@[_editImageView.width,_deleteImageView.width,_shareImageView.width,_addNewImageView.width]);            make.height.equalTo(@[_editImageView.height,_deleteImageView.height,_shareImageView.height,_addNewImageView.height]);        }];        [_deleteButton makeConstraints:^(MASConstraintMaker *make) {            make.right.equalTo(self.right).offset(-100);            make.width.equalTo(_addNewImageView.mas_width).offset(60);            make.height.and.bottom.and.top.equalTo(_deleteImageView);        }];        [_shareImageView makeConstraints:^(MASConstraintMaker *make) {            make.left.equalTo(_editImageView);            make.right.equalTo(_weixinButton.left).offset(-10);            make.bottom.equalTo(_addNewImageView.top).offset(-100);            make.width.equalTo(@[_editImageView.width,_deleteImageView.width,_shareImageView.width,_addNewImageView.width]);            make.height.equalTo(@[_editImageView.height,_deleteImageView.height,_shareImageView.height,_addNewImageView.height]);        }];        //微信 微博 qq        [_weixinButton makeConstraints:^(MASConstraintMaker *make) {            make.right.equalTo(_friendsCircleButton.left).offset(-10);            make.width.equalTo(_friendsCircleButton.width);            make.width.equalTo(_weixinButton.width);            make.height.and.bottom.and.top.equalTo(_shareImageView);        }];        [_friendsCircleButton makeConstraints:^(MASConstraintMaker *make) {            make.right.equalTo(_weiboButton.left).offset(-10);            make.width.equalTo(@[_friendsCircleButton.width,_weiboButton.width]);            make.height.and.bottom.and.top.equalTo(_weixinButton);        }];        [_weiboButton makeConstraints:^(MASConstraintMaker *make) {            make.right.equalTo(self.right).offset(-30);            make.width.equalTo(@[_friendsCircleButton.width,_weiboButton.width]);            make.height.and.bottom.and.top.equalTo(_shareImageView);        }];        [_qqButton makeConstraints:^(MASConstraintMaker *make) {            make.left.and.right.and.width.and.height.equalTo(_weixinButton);            make.top.equalTo(_shareImageView.bottom).offset(10);            //make.bottom.equalTo(_addNewImageView.top).offset(-80);        }];        [_addNewImageView makeConstraints:^(MASConstraintMaker *make) {            make.left.and.right.equalTo(_editImageView);            make.bottom.equalTo(self.bottom).offset(-50);            make.width.equalTo(@[_editImageView.width,_deleteImageView.width,_shareImageView.width,_addNewImageView.width]);            make.height.equalTo(@[_editImageView.height,_deleteImageView.height,_shareImageView.height,_addNewImageView.height]);        }];        [_addNewButton makeConstraints:^(MASConstraintMaker *make) {            make.right.equalTo(self.right).offset(-100);            make.width.equalTo(_addNewImageView.mas_width).offset(60);            make.height.and.bottom.and.top.equalTo(_addNewImageView);        }];

编译后如下图
这里写图片描述


未完待续

0 0
原创粉丝点击