Hey guys I have the following:
- (void)viewDidLoad
{
[super viewDidLoad];
// SET BACKGROUND OF APPLICATION ON STARTUP
[self.view setBackgroundColor: [self colorWithHexString:@"88C800"]];
// Do any additional setup after loading the view, typically from a nib.
}
-(UIColor*)colorWithHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// strip 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString length] != 6) return [UIColor grayColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
This will change the background color of my view controller (root)
to the specified hex color I supply. My issue is, when navigating to another view controller
, how do I get the above function to effect all of my view controllers
? This code is within my ViewController.m
Not to sure what I need to do, suggestions and thoughts?
I'd suggest two things:
colorWithHexString:
a category class method on UIColor
. Then you can access the method from any class that imports your category .h file.MyBaseViewController
. Have this class extend UIViewController
then have all of your app's view controllers extend MyBaseViewController
. Put all common setup in the base class. If you also need some table view controllers then create a MyBaseTableViewController
class with similar code.