如何制作不透明的UIButton?

时间:2013-03-13 06:34:48

标签: ios xcode ipad

我想在我的应用程序按钮的右上角创建一个带有文本的不透明按钮。我尝试了以下方法来实现不透明度,但无法获得不透明度。

通过以下方法,我在后台获得了白色按钮

1.      UIButton *Button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
        Button.frame = CGRectMake(offset, height/4, buttonWidth, buttonHeight);
        [Button setTitle:ButtonLabel forState:UIControlStateNormal];
         Button.backgroundColor = [UIColor clearColor];
        [Button addTarget:self action:@selector(action:)forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:Button];

2.  CALayer *layer = Button.layer;
    layer.backgroundColor = [[UIColor clearColor] CGColor];
    layer.borderColor = [[UIColor blackColor] CGColor];
    layer.cornerRadius = 8.0f;
    layer.borderWidth = 2.0f;

我知道这可能是重复的,但链接

中给出的建议没有任何效果

另请注意如何在按钮右下角设置标题。

3 个答案:

答案 0 :(得分:3)

对于 opacity ,您可以设置setAlpha

UIButton属性

对于文字对齐,您可以使用contentHorizontalAlignmentcontentVerticalAlignment

以下是经过修改的代码:

UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Button.frame = CGRectMake(10, 10, 200, 100);
[Button setTitle:@"myButton" forState:UIControlStateNormal];
Button.backgroundColor = [UIColor clearColor];
[Button setAlpha:0.42];
Button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
Button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
[Button addTarget:self action:@selector(action:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Button];

答案 1 :(得分:1)

当您不需要特殊样式的RoundRect,InfoDark等时,请使用UIButtonTypeCustom buttonType。您将能够使用Quartz添加边框颜色和/或圆角

 #import <QuartzCore/QuartzCore.h>

_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
_button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
_button.layer.borderColor = [[UIColor blackColor] CGColor];
_button.layer.borderWidth = 1;
_button.layer.cornerRadius = 10;

答案 2 :(得分:1)

只需使用自定义类型而不是UIButtonTypeRoundedRect创建按钮。

UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];