简体   繁体   English

iphone sdk透明子视图背景

[英]iphone sdk transparent subview background

I have a main view with a picture on it. 我有一个主视图,上面有一张图片。

I am trying to add a subview with [self.view addSubview:view2]; 我正在尝试使用[self.view addSubview:view2];添加子视图[self.view addSubview:view2]; but I want the view2 background to be transparent. 但我希望view2背景透明。 Have tried opaque=no and background color to clearcolor and also tried to subclass a uiview and rewrite the drawrect with: 尝试了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

But still doesn't display the background of the subview transparent... any ideas? 但仍然没有显示子视图的背景透明......任何想法?

Try: 尝试:

view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;

Is the view being loaded from a nib file? 视图是从nib文件加载的吗? If so, the -initWithFrame: won't be called; 如果是这样,则不会调用-initWithFrame: -initWithCoder: will be called instead. -initWithCoder:将被调用。 A better place to do this initialization might be in -viewDidLoad . 进行此初始化的更好位置可能是在-viewDidLoad But setting the background color to [UIColor clearColor] should definitely do the trick. 但是将背景颜色设置为[UIColor clearColor]绝对应该可以解决问题。

Try coloring the subview's background with a 0.0 for Alpha. 尝试使用0.0 for Alpha为子视图的背景着色。 That should make it completely transparent. 这应该使它完全透明。

Something like this: 像这样的东西:

UIColor *myUIColor = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha:0.0];

In the function 在功能

- (void)drawRect:(CGRect)rect 

try to update 尝试更新

    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);

to

  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

This link might help you http://www.cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html 这个链接可能会帮助你http://www.cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html

I've had some cases where ... addSubview:clearView] seemed to reset the background color of clearView ( WTF! ) to something not clear. 我有一些情况... addSubview:clearView]似乎将clearViewWTF! )的背景颜色重置为不清楚的东西。 I added a 我加了一个

[clearView setBackgroundColor:nil];

somewhere after that and it seemed to help. 在那之后的某个地方似乎有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM