//displayView
UIView *displayView = [UIView new];
displayView.backgroundColor = [UIColor blackColor];
[self addSubview:displayView];
//keybordView
UIView *keybordView = [UIView new];
[self addSubview:keybordView];
[displayView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self);
make.height.equalTo(keybordView).multipliedBy(.3f);
}];
[keybordView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(displayView.mas_bottom);
make.left.right.bottom.equalTo(self);
}];
//设置显示位置的数字为0
UILabel *displayNum = [[UILabel alloc]init];
[displayView addSubview:displayNum];
displayNum.text = @"0";
displayNum.font = [UIFont fontWithName:@"HeiTi SC" size:70];
displayNum.textColor = [UIColor whiteColor];
displayNum.textAlignment = NSTextAlignmentRight;
[displayNum mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(displayView).with.offset(-10);
make.bottom.equalTo(displayView).with.offset(-10);
}];
UIView *testView = [UIView new];
testView.backgroundColor = [UIColor redColor];
[keybordView addSubview:testView];
[testView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(keybordView.mas_width).multipliedBy(.25f);
make.height.equalTo(keybordView.mas_height).multipliedBy(.2f);
make.left.equalTo(keybordView.mas_left);
make.baseline.equalTo(keybordView.mas_baseline).multipliedBy(0.2);
}];
//我的理解mas_baseline是view的底部距离顶部的距离。注意参照点是顶部
//make.baseline.equalTo(keybordView.mas_baseline).multipliedBy(1);
//等价testView的底部距离keybordView顶部的距离 = keybordView的底部距离距离顶部的距离 乘以 1
注解如下:
displayView 黑色区域
keybordView 图中白色区域
现在要把红色区域添加到白色区域
详细代码如下:
UIView *testView = [UIView new];
testView.backgroundColor = [UIColor redColor];
[keybordView addSubview:testView];
[testView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(keybordView.mas_width).multipliedBy(.25f);
make.height.equalTo(keybordView.mas_height).multipliedBy(.2f);
make.left.equalTo(keybordView.mas_left);
make.baseline.equalTo(keybordView.mas_baseline).multipliedBy(0.2);
}];
主要解释如下这句代码:
make.baseline.equalTo(keybordView.mas_baseline).multipliedBy(0.2);
}];
- mas_baseline是view的底部距离顶部的距离。注意参照点是顶部
因为
make.height.equalTo(keybordView.mas_height).multipliedBy(.2f);
testView的高度等于keybordView的高度乘以0.2
所以
- make.baseline.equalTo(keybordView.mas_baseline).multipliedBy(.2f);
根据代码判断:
- 等价testView的底部距离keybordView顶部的距离 = keybordView的底部距离距离顶部的距离 乘以 0.2
刚好和高度吻合
所以testView显示的位置就刚好在keybordView的左上角的位置。顶部和keyboardView的顶部吻合。