0

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

通过以下方法,我在背景中得到了白色按钮

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;

我知道这可能是重复的,但是链接中给出的建议没有任何效果

另外请建议如何在按钮的右下角设置标题。

4

3 回答 3

2

对于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];
于 2013-03-13T07:05:44.803 回答
1

UIButtonTypeCustom当您不需要特殊样式的 RoundRect、InfoDark 等时使用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;
于 2013-03-13T07:02:12.303 回答
1

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

UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
于 2013-03-13T07:04:51.177 回答