2012-05-11 31 views
5

Vì vậy, tôi tạo một nút. Tôi muốn nó có các góc tròn với các tập tin .png tròn Tôi có thể đặt hình nền ok. Tôi có thể làm tròn các góc ok. Tuy nhiên, khi tôi đặt hình nền, các góc tròn sẽ biến mất.UIButton với các góc tròn sử dụng bộ vuôngBackBackgroundImage

Tôi đã làm điều này một lần trước một thời gian dài trước đây nhưng dường như không thể làm cho nó hoạt động trở lại. Điều này có thể giải quyết như thế nào?

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

Trả lời

18

Thêm thuộc tính maskToBounds để nút của bạn.

button1.layer.masksToBounds = YES; 
+0

yea duh thanks! – Rick

+0

Tuyệt vời. Giải quyết vấn đề của tôi. +1 –

+0

cảm ơn bạn rất nhiều! – mikezs