arnoapp
arnoapp

Reputation: 2506

UIView drawRect Method with options

I want to create an UIView Class that you can initialise with several options.

I think the problem is the drawRect() method. Is it possible to initialise the class with a certain option like

[[MyUIView alloc]initWithColor:[UIColor blueColor]]

I read that I should never call the drawRect() method without initialising an object.

So how can I realize the statement above without calling it manually?

What I have tried so far:

I thought that I could just initialize the View and then call Method

[MyView changeColorTo:green]

which tries to redraw the View however I couldn't get that to work.

Maybe it's important that I draw with coreGraphics (with a certain color, which should be selectable) a rounded rectangle in the drawRect() method.

How can I implement my desired behaviour?

Upvotes: 0

Views: 291

Answers (1)

user529758
user529758

Reputation:

call

[self setNeedsDisplayInRect:aCGRect];

that calls drawRect: for you.

Edit on the color property:

@interface MyView: UIView

@property (nonatomic, strong) UIColor *drawColor;

@end

@implementation MyView

@synthesize drawColor;

- (id)initWithWhatever
{
    self.drawColor = [UIColor infraRedColor];
    [self setNeedsDisplayInRect:aRect];
}

- (void)drawRect:(CGRect)r
{
    // use self.drawColor
}

@end

Upvotes: 1

Related Questions