將子圖層的顏色設置爲想要按鈕的顏色(不要設置按鈕本身的背景顏色),並將其矩形插入到按鈕的矩形中,
- (void) awakeFromNib {
self.layer.cornerRadius = 6.0f;
self.layer.borderWidth = 4.0f;
self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
CALayer *sub = [CALayer new];
sub.frame = CGRectInset(self.bounds, 4, 4);
sub.backgroundColor = [UIColor redColor].CGColor;
[self.layer addSublayer:sub];
}
另一種方式來做到這一點,如果你想要的背景顏色過於圓潤,這將更好地工作,是用背景色self.layer和子層兩者。在這種情況下,所有人都需要使用邊框。
- (void) awakeFromNib {
self.layer.cornerRadius = 6.0f;
self.tintColor = [UIColor whiteColor]; // make white text
self.layer.backgroundColor = [[UIColor colorWithWhite:1.0f alpha:0.4] CGColor];
CALayer *sub = [CALayer new];
sub.cornerRadius = 4.0f;
sub.frame = CGRectInset(self.bounds, 4, 4);
sub.backgroundColor = [UIColor blueColor].CGColor;
[self.layer addSublayer:sub];
}