例子中btn为黄色区域,高20,img为绿色区域,高10,原始布局img居中,上下边距分别为5。
可见:
edge指向view中心为正,指向view外围为负;
当edge设为(5,0,0,0)含义为,top边界向内缩5,图片y方向空间减少5,剩余15,img在此区域中居中,扣除高度10,因此上下边距为2.5,而上方加上内缩的5,因此最终效果是上7.5,下2.5;
当edge设为(5,0,5,0)含义为,top和bottom边界分别向内缩5,图片y方向空间减少10,剩余10,img在此区域中居中,刚好。效果与未设置相同;
当edge设为(9,0,9,0)含义为,top和bottom边界分别向内缩9,图片y方向空间减少18,剩余2,img被压缩,只剩2;
当edge设为(-5,0,5,0),上边距向上5,下边距向上5,相当于高度不变,向上平移了5,可见下边距为10;
(-10,0,0,0)上边距上拉10,高度变为30,img在此居中,可见下边距为10,因此正好与(-5,0,5,0)相同;
- (void)createBtn {
int top = 5; int bottom = 5;
for (int i = 0 ; i < 12; i ++) {
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake((50*i)%300,30 + 50*(i/6), 40, 15)];
lbl.font = [UIFont systemFontOfSize:8];
[self.view addSubview:lbl];
UIButton *btn = [self makebtn];
btn.frame = CGRectMake((50*i)%300,50 + 50*(i/6), 40, 20);
btn.backgroundColor = [UIColor colorWithRed:230/255. green:240/255. blue:0 alpha:1];
if(i== 0) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}
if(i== 1) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(top, 0, 0, 0)];
}
if(i== 2) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, bottom, 0)];
}
if(i== 3) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(top, 0, bottom, 0)];
}
if(i== 4) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(-top, 0, 0, 0)];
}
if(i== 5) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, -bottom, 0)];
}
if(i== 6) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(-top, 0,-bottom, 0)];
}
if(i== 7) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(top, 0,-bottom, 0)];
}
if(i== 8) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(-top, 0,bottom, 0)];
}
if(i== 9) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(-2*top, 0,0, 0)];
}
if(i== 10) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0,-2*bottom, 0)];
}
if(i== 11) {
[btn setImageEdgeInsets:UIEdgeInsetsMake(2*top-1, 0,2*bottom-1, 0)];
}
lbl.text = [NSString stringWithFormat:@"%d,%d,%d,%d",(int)btn.imageEdgeInsets.top,(int)btn.imageEdgeInsets.left,(int)btn.imageEdgeInsets.bottom,(int)btn.imageEdgeInsets.right];
[self.view addSubview:btn];
}
}
网友评论