autolayout - sizeClass - Masonry - 6

来源:互联网 发布:西安软件公寓地址 编辑:程序博客网 时间:2024/06/15 23:27

打算做一个仿微信对话的聊天界面,并尽可能用Masonry去实现里面所有能实现的地方:

键盘:

#import "Masonry.h"#import "QQChating.h"#import "KeyBordView.h"#import "UIImage+StrethImage.h"@interface KeyBordView () <UITextFieldDelegate>@property (nonatomic, strong) UIButton* voiceButton;@property (nonatomic, strong) UIButton* imageButton;@property (nonatomic, strong) UIButton* addButton;@property (nonatomic, strong) UIButton* speakButton;@property (nonatomic, strong) UITextField* textFiled;@property (nonatomic, strong) UIImageView* backImageView;@end@implementation KeyBordView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];        if (self)    {        [self setBackgroundColor:[UIColor blackColor]];        [self initialData];    }        return self;}#pragma mark - 初始化通UIButton- (UIButton*)buttonWithNormal:(NSString* )normal hightLight:(NSString* )hightLight                       action:(SEL)action{    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];    [btn setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];    [btn setImage:[UIImage imageNamed:hightLight] forState:UIControlStateHighlighted];    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];        return btn;}#pragma mark - 初始化数据- (void)initialData{    UIView* superview = self;        self.backImageView = [UIImageView new];    [self.backImageView setBackgroundColor:[UIColor blackColor]];    [superview addSubview:self.backImageView];        self.voiceButton = [self buttonWithNormal:@"chat_bottom_voice_nor.png"                                   hightLight:@"chat_bottom_voice_press.png"                                       action:@selector(voiceBtnPress:)];    [superview addSubview:self.voiceButton];            self.textFiled = [UITextField new];    self.textFiled.returnKeyType = UIReturnKeySend;    self.textFiled.font = [UIFont fontWithName:@"HelveticaNeue" size:14];    self.textFiled.placeholder = @"请输入";    self.textFiled.background = [UIImage imageNamed:@"chat_bottom_textfield.png"];    self.textFiled.delegate = self;    [superview addSubview:self.textFiled];            self.imageButton = [self buttonWithNormal:@"chat_bottom_smile_nor.png"                                   hightLight:@"chat_bottom_smile_press.png"                                       action:@selector(imageBtnPress:)];    [self addSubview:self.imageButton];            self.addButton = [self buttonWithNormal:@"chat_bottom_up_nor.png"                                 hightLight:@"chat_bottom_up_press.png"                                     action:@selector(addBtnPress:)];    [self addSubview:self.addButton];            self.speakButton = [self buttonWithNormal:nil hightLight:nil action:@selector(speakBtnPress:)];    [self.speakButton setTitle:@"按住说话" forState:UIControlStateNormal];    [self.speakButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];    [self.speakButton addTarget:self action:@selector(touchDown:)               forControlEvents:UIControlEventTouchDown];    [self.speakButton setTitleColor:[UIColor redColor]                           forState:(UIControlState)UIControlEventTouchDown];    [self.speakButton setBackgroundColor:[UIColor whiteColor]];    self.speakButton.hidden = YES;    [self addSubview:self.speakButton];            [self.backImageView mas_makeConstraints:^(MASConstraintMaker* make){                make.size.mas_equalTo(superview);        make.center.mas_equalTo(superview);            }];            [self.voiceButton mas_makeConstraints:^(MASConstraintMaker* make){                make.centerY.mas_equalTo(superview.mas_centerY);        make.left.mas_equalTo(superview.mas_left).with.offset(10);        make.width.equalTo(@33);        make.height.equalTo(@33);            }];            [self.textFiled mas_makeConstraints:^(MASConstraintMaker* make){            make.centerY.mas_equalTo(superview);        make.left.mas_equalTo(self.voiceButton.mas_right).with.offset(10);        make.right.mas_equalTo(self.imageButton.mas_left).with.offset(-10);        make.top.mas_equalTo(superview.mas_top).with.offset(5);        make.bottom.mas_equalTo(superview.mas_bottom).with.offset(-5);            }];        [self.imageButton mas_makeConstraints:^(MASConstraintMaker* make){            make.centerY.mas_equalTo(superview);        make.left.mas_equalTo(self.textFiled.mas_right).with.offset(10);        make.right.mas_equalTo(self.addButton.mas_left).with.offset(-10);        make.width.equalTo(@33);        make.height.equalTo(@33);        }];        [self.addButton mas_makeConstraints:^(MASConstraintMaker* make){            make.centerY.mas_equalTo(superview);        make.right.mas_equalTo(superview.mas_right).with.offset(-10);        make.width.equalTo(@33);        make.height.equalTo(@33);        }];    }


调用代码也是非常地简单:

    self.keyBordView = [KeyBordView new];    [self.view addSubview:self.keyBordView];    [self.keyBordView mas_makeConstraints:^(MASConstraintMaker* make){                make.width.mas_equalTo(superview.mas_width);        make.height.mas_equalTo(@44);        make.centerX.mas_equalTo(superview);        make.bottom.mas_equalTo(superview.mas_bottom);        }];


效果如下:

竖屏



横屏:

代码除了间距,完全没有相关的写死代码,实现起来非常简单和灵活,代码也非常地简洁。

0 0