NavigationBar Layer Gradient
To add gradient layer to Navigationbar follow below code snippet:
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navigationBar];
// navigationBar.tintColor = [UIColor colorWithRed:.7 green:.5 blue:.2 alpha:1];
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"Nav Bar"];
[navigationBar pushNavigationItem:item animated:YES];
// Set gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 320, 44);
NSMutableArray *cgColors = [[NSMutableArray alloc] init];
[cgColors addObject:(id)[[UIColor colorWithRed:1.0
green:0.0
blue:0.0
alpha:0.2] CGColor]];
[cgColors addObject:(id)[[UIColor colorWithRed:1.0
green:0.0
blue:0.0
alpha:0.6] CGColor]];
[cgColors addObject:(id)[[UIColor colorWithRed:1.0
green:0.0
blue:0.0
alpha:1.0] CGColor]];
gradient.colors = cgColors;
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 42, 320, 2);
layer.backgroundColor = [[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.7] CGColor];
[navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[[[navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:gradient atIndex:0];
[[[navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:layer atIndex:2];
Final Output will look like:
I followed stackoverflow :
Comments
Post a Comment