UIView *redV = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
redV.backgroundColor = [UIColor redColor];
[self.view addSubview:redV];
// Inset 改变大小,- (向外扩张)为变大,+(向内收缩)为减小
CGRect rect1 = CGRectInset(redV.frame, -10, 10);
UIView *greenV = [[UIView alloc] initWithFrame:rect1];
greenV.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
[self.view addSubview:greenV];
效果:

// offSet 只平移位置,不改变大小
CGRect rect2 = CGRectOffset(redV.frame, -10, -10);
UIView *blueV = [[UIView alloc] initWithFrame:rect2];
blueV.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
[self.view addSubview:blueV];
效果:

// UIEdgeInsetsInsetRect 在原先的rect上内切出另一个rect出来,-为变大,+为变小
CGRect rect3 = UIEdgeInsetsInsetRect(redV.frame, UIEdgeInsetsMake(-20, -20, -20, -20));
UIView *yellowV = [[UIView alloc] initWithFrame:rect3];
yellowV.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.5];
[self.view addSubview:yellowV];
效果:

网友评论