2013-08-22 46 views
1

我想在触摸时用UIButton填充颜色。该方法可以在操作方法在里面触摸时用一种颜色填充UIButton

- (IBAction)bocClick:(UIButton *)sender { 
    [sender setBackgroundColor:[UIColor blackColor]]; 
} 

实现我得到像下面

enter image description here

这样的背景下是不够的,我需要的按钮,我需要有一个效果,像..

enter image description here

回答

2

您使用的是圆形rect按钮,这是有点复杂,CU stomize。

您可以创建一个自定义类型按钮并使用背景图像,或者创建一个自定义类型按钮并绕过角点并自行设置该图层的背景colour

,或者您也可以做波纹管: -

您可以添加的UIView或拉布勒这样的波纹管例如: -

- (IBAction)bocClick:(UIButton *)sender { 

UILabel *lbl2=[[UILabel alloc]initWithFrame:_btn.bounds]; 
[email protected]"Button"; 
lbl2.textAlignment=UITextAlignmentCenter; 
lbl2.font=[UIFont boldSystemFontOfSize:16]; 
lbl2.backgroundColor=[UIColor redColor]; 
lbl2.layer.cornerRadius = 5; 
lbl2.layer.masksToBounds = YES; 
[sender addSubview:lbl2]; 
} 

输出是: -

enter image description here

东西similor: -

iOS - Setting UIButton background color only colors the corners

+0

是否有任何方法可以添加一个视图在我们可以从“sender.frame”获得的按钮框架上,并将它放在按钮上以产生类似的效果......我不需要再次修改所以? – Raon

+0

是的,你可以添加视图作为巴顿的subview在che TouchUpInside方法 –

+0

你能解释一下吗? – Raon

1

你只需要设置tintColor属性: [sender setTintColor:[UIColor redColor]];

所以你当高亮颜色TouchUpInside。如果您还想更改背景颜色,则更为复杂。然后,你必须使用Quartz

+0

这将无法正常工作,我已经尝试:( 谢谢虽然 – Raon

1

只有做到这一点

的UIButton * bocClick = [UIButton的buttonWithType:UIButtonTypeCustom]。

1

请尝试更改按钮类型为UiButtonTypeCustom。 请看下面的图片。 enter image description here 希望它能帮助你。