0
votes

im quite new to IOS; and i made some gradient etc via the drawRect method, now, if i want to implement it, i subclass it to the view. But i was thinking, is there any way i can call the method drawRect and make it set the background for the view, from a Subclass of the ViewController ?

Layout is like this:

BackgroundColor.h

#import <UIKit/UIKit.h>

@interface BackgroundColor : UIView

@end

BackgroundColor.m

#import "BackgroundColor"

@implementation BackgroundColor___Fuschia


- (void)drawRect:(CGRect)rect
{
...
}

@end

ViewControllerNav.h

#import <UIKit/UIKit.h>

@interface ViewControllerNav : UIViewController

@end

ViewControllerNav.m

@interface ViewControllerNav ()

@end

@implementation ViewControllerNav

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I Assume i have to import BackgroundColor and init the method or ?

(PS: My drawRect method uses CGRect frame = rect; so i need the view size passed i asume ?)

Any help is apriciated

1
Why are you using drawRect to make a background color? Is there something complicated about this "color"?rdelmar
This is not the correct approach. Instead, you should make a UIView subclass that has several color properties (or an array, or even a gradient object) and draws its background according to that. Don't make a separate implementation for each background. If you want, you can define some "standard" ones in the UIView subclass. Then you can do whatever you like with it from the view controller.borrrden
Thanks a bunch, il do thatuser2559108

1 Answers

1
votes

To answer your original question, you never call drawRect: directly. You call

[self.view setNeedsDisplay];

and the system takes care of calling your drawRect: at the appropriate time.