2012-05-11 34 views
5

所以我創建一個按鈕。我希望它有圓角與出來使圓形.png文件 我可以設置背景圖像確定。我可以繞過角落。但是,當我設置背景圖像時,圓角消失。使用方形圓角的UIButtonBackgroundImage

很久以前我曾經這樣做過一次,但似乎無法讓它再次工作。如何才能做到這一點?

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button1 addTarget:self action:@selector(goSpecials) 
    forControlEvents:UIControlEventTouchUpInside]; 
[button1 setTitle:@"Specials" forState:UIControlStateNormal]; 
button1.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:(15.0)]; 
[button1 setTitleColor:[self colorWithHexString:buttonColor] forState:UIControlStateNormal]; 
button1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"button.png"]]; 
//[button1 setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 
button1.frame = CGRectMake(20.0, 232.0, 135.0, 32.0); 
button1.layer.borderColor = [UIColor blackColor].CGColor; 
button1.layer.borderWidth = 0.5f; 
button1.layer.cornerRadius = 8.0f; 
[self.view addSubview:button1]; 

button.png: enter image description here

回答

18

的maskToBounds屬性添加到您的按鈕。

button1.layer.masksToBounds = YES; 
+0

yeah duh thanks! – Rick

+0

太好了。解決了我的問題。 +1 –

+0

非常感謝你! – mikezs