Replace White Background in Cordova iOS Mobile App with a Hex Color Background

If you have gone thorugh this article, we replaced white background with black background. In order to change the background color to a color of our choosing, we can use the code snippent below in MainViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	self.webView.opaque=NO;
	NSString *hexColor = @"#33CCFF";
	UIColor *theColor = [self colorFromHexString:hexColor];
    self.webView.backgroundColor = theColor;
}

// Assumes input like "#FF00FF00" (#AARRGGBB)
- (UIColor *)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    float alphaValue = 1.0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; // bypass '#' character
    [scanner scanHexInt:&rgbValue];
    if (hexString.length==9) {
        alphaValue = ((rgbValue & 0xFF000000) >> 24)/255.0;
    }
    return [UIColor colorWithRed:((rgbValue & 0x00FF0000) >> 16)/255.0 green:((rgbValue & 0x0000FF00) >> 8)/255.0 blue:(rgbValue & 0x000000FF)/255.0 alpha:alphaValue];
}

Leave A Comment

Your email address will not be published.