2014-08-28 182 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]; 

你不需要子视图或其他任何东西。