2013-03-13 5 views
0

Я хотел создать непрозрачную кнопку с текстом в правом углу кнопки для моего приложения. Я пробовал следующие способы достижения непрозрачности, но не смог получить непрозрачность.Как сделать непрозрачный UIButton?

С ниже подходит я получаю белую кнопку на фоне

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; 

Я знаю, что это возможно дубликат, но ничего не работает из предложений, приведенных в ссылках

Также предложите, как установить название в правом нижнем углу кнопки.

+0

Если вы хотите непрозрачную кнопку, почему вы даете ей фон [UIColor clearColor]? – Premsuraj

+0

[UIColor clearColor] сделает вашу кнопку прозрачной. просто удалите его, и вы можете получить непрозрачную кнопку –

+0

Я попытался использовать значение альфа, но не удалось. У вас есть предложения по использованию прозрачности. – DNamto

ответ

3

Для opacity, вы можете установить setAlpha свойство UIButton

Для выравнивания текста, вы можете использовать contentHorizontalAlignment и contentVerticalAlignment

Вот отредактированный код:

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

UIButtonTypeCustom Использование buttonType, когда вы не нуждаются в особом стиле ROUNDRECT, InfoDark и т.д. Вы сможете добавить цвет границы и/или круглые углы с кварцем

#import <QuartzCore/QuartzCore.h> 

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

просто создать кнопку с пользовательский тип вместо UIButtonTypeRoundedRect.

+0

для выравнивания по правому краю. Фрагмент A-Live будет работать , – KDeogharkar