can't get my UIButton to show up as semi-transparent
I'm adding a UIButton as a subView in a popup and I can't get it to show up semi-transparently. The background for the main view of the popup does show up semi-transparently, but this UIButton 开发者_如何学编程stays fully opaque. Can someone tell my why the code below is not working? Thanks.
UIButton* checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
checkButton.frame = CGRectMake(187, 288, 30, 30);
[checkButton setTitle:@"?" forState:UIControlStateNormal];
checkButton.opaque = NO;
[checkButton setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:0.3]];
[checkButton setTitleColor: [UIColor colorWithRed: 51 / 255.0 green:0 blue: 153 / 255.0 alpha:0.5] forState: UIControlStateNormal];
[checkButton addTarget:self action:@selector(check) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:checkButton];
The background for a RoundedRect button is the rectangle in which it is located, and not the button itself. Try changing the background color to red or something equally visible and you will see that the background is only visible between the rounded corners of the button and the rectangular frame in which the button is set. Sadly, you cannot change the color of the button, which is what most people think they are doing when they change the backgroundColor
property.
To change the button color, you will need to use UIButtonTypeCustom
.
try setting your UIColor to
myButton.layer.backgroundColor = [UIColor x..]
you'll need a
#import <QuartzCore/QuartzCore.h>
im pretty sure the colour in the rounded rect button is in the layer, not in the regular background, this is why people struggle to change its colour..
alternately, put the alpha on the views colour back to 1 and hit the views alpha property, that'll certainly do the lot in one move.
PengOne is right – setting the background color doesn't change the color of the rounded rect. It only affects the corners around the rect.
But you can also just set the button's alpha property to something less than one. Try 0.7 to make the view semi-transparent.
Try using UIButtonTypeCustom
instead UIButtonTypeRoundedRect
as the button type.
UIButtonTypeRoundedRect is weird. Setting background /alpha etc on it only changes the background of the rectangle into which the button is rendered.
You could change the button type to UIButtonTypeCustom and then use QuartzCore to set cornerRadius to make it appear rounded.
精彩评论