views:

141

answers:

1

I have a view with a subview whose alpha and backgroundcolor I would like to change through the use of declared properties. I have written some code as below and it works fine to change the alpha, but the background color is not changed when the property is set to a new value. Any help would be appreciated please.

    @interface MyView : UIView {
     float viewAlpha;
     UIColor *viewColor;
    }
    @property (nonatomic, assign, readwrite) float viewAlpha;
    @property (nonatomic, retain, readwrite) UIColor *viewColor;



    @implementation MyView

    @synthesize viewAlpha, viewColor;

    - (id)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {

        self.viewAlpha = 1.0; 
        self.viewColor = [UIColor whiteColor];

        UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,50)];
        infoView.alpha = self.viewAlpha;
           infoView.backgroundColor = self.viewColor;
        [self addSubview:infoView];
        [infoView release];
        }


return self;
}
+2  A: 

You might need to write a custom setter and flag that your view needs to redraw when it changes. It'd be a good idea to do this for both the alpha and the color.

Benno
Hi Benno. Writing a custom setter with a redraw request is something I had in mind, but it seems odd that one can change the background color form within a view without calling redraw but cannot set it from outside.
Run Loop
Yeah, I'd expect both to fail. I'm unsure as to why one works, but I suspect it's because one's a static value and the other is an object reference.
Benno