2014-08-28 96 views
0

我正在嘗試使邊框透明UIView。問題是邊界將永遠是透明的,我怎樣才能使它不透明?如何使用非透明邊框創建透明UIView

這是我的代碼

- (void) viewDidLoad:(BOOL)animated 
{ 

    [super viewDidLoad:animated]; 

    _transparentView.alpha = 0.5f; 
    _transparentView.layer.borderColor = [UIColor whiteColor].CGColor; 
    _transparentView.layer.borderWidth = 2.0f; 
} 
+0

你的解決方案不起作用adan – 2014-08-28 14:07:54

回答

2

您可以通過添加透明視圖爲另一個視圖的子視圖等爲波紋管

_transparentView.alpha = 0.5f; 

_MainView.backgroundColor = [UIColor clearColor]; 
_MainView.layer.borderColor = [UIColor whiteColor].CGColor; 
_MainView.layer.borderWidth = 2.0f; 
[_MainView addSubview:_transparentView]; 
2

做到這一點,您可以通過設置的backgroundColor實現這一目標視圖,以透明色的:

_transparentView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.5]; 
_transparentView.layer.borderColor = [UIColor whiteColor].CGColor; 
_transparentView.layer.borderWidth = 2.0f; 

這將使視圖TRA的背景溫暖的綠色。

1
-(void)setRoundedView:(UIView *)vW{ 
    CALayer *image = vW.layer; 
    [image setCornerRadius:5]; 
    [image setBorderWidth:1]; 
    image.masksToBounds = YES; 
    image.borderColor = [UIColor colorWithRed:202.0/255.0 green:202.0/255.0 blue:202.0/255.0 alpha:1].CGColor; 
} 
+0

Hiren,你的代碼給了我四捨五入的透明邊框,我不在乎它們是否圓整,但我需要透明視圖的不透明邊框。 – 2014-08-28 14:10:37

+0

更改邊框顏色 – Hiren 2014-08-29 05:02:20

2

背景就設定一個明確的顏色:

// set the border color 
_transparentView.layer.borderColor = [UIColor whiteColor]; 
// set the border width 
_transparentView.layer.borderWidth = 3.0f; 
// want rounded corners? set this 
_transparentView.layer.cornerRadius = 5.0f; 

// make the background clear color to be fully transparent 
_transparentView.backgroundColor = [UIColor clearColor]; 

你不需要子視圖或其他任何東西。