[英]iphone sdk transparent subview background
我有一個主視圖,上面有一張圖片。
我正在嘗試使用[self.view addSubview:view2];
添加子視圖[self.view addSubview:view2];
但我希望view2背景透明。 嘗試了opaque = no和背景色到clearcolor,並嘗試將uiview子類化並用以下內容重寫drawrect:
#import "TransparentView.h"
@implementation TransparentView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor clearColor]];
self.opaque=NO;
self.clearsContextBeforeDrawing=YES;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
}
@end
但仍然沒有顯示子視圖的背景透明......任何想法?
嘗試:
view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
視圖是從nib文件加載的嗎? 如果是這樣,則不會調用-initWithFrame:
-initWithCoder:
將被調用。 進行此初始化的更好位置可能是在-viewDidLoad
。 但是將背景顏色設置為[UIColor clearColor]
絕對應該可以解決問題。
嘗試使用0.0 for Alpha為子視圖的背景着色。 這應該使它完全透明。
像這樣的東西:
UIColor *myUIColor = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha:0.0];
在功能
- (void)drawRect:(CGRect)rect
嘗試更新
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
至
const CGFloat BACKGROUND_OPACITY = 0.85; //Note: update this value to what you need
CGContextSetRGBFillColor(context, 1, 1, 1, BACKGROUND_OPACITY); // You can change 1,1,1 to the needed values
這個鏈接可能會幫助你http://www.cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
我有一些情況... addSubview:clearView]
似乎將clearView
( WTF! )的背景顏色重置為不清楚的東西。 我加了一個
[clearView setBackgroundColor:nil];
在那之后的某個地方似乎有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.