2013-03-13 39 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 2013-03-13 06:55:11

+0

[UIColor clearColor]將使您的按鈕透明。只要刪除那一個,你可以得到不透明的按鈕 – 2013-03-13 07:02:27

+0

我試圖使用alpha值,但沒有成功。你有任何建議如何去不透明 – DNamto 2013-03-13 07:03:51

回答

3

對於opacity,您可以設置的UIButton

對於文本對齊setAlpha屬性,可以使用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

使用UIButtonTypeCustom按鈕類型時,你不需要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 2013-03-13 07:25:19