NavigationBar layer gradient not working well - iphone

I am adding gradient to navigation Bar' layer and it is working fine.
Issue arrive when I push another view Controller and pop view Controller. Parent view Controller has right Bar Button Item whose color dims when I pop View Controller.
My code is
CGRect navFrame = self.navigationController.navigationBar.frame;
navFrame.origin.y = 0.0f;
[self.navigationController.navigationBar.layer insertSublayer:[AddGradient addGradientToNavigationBar:navFrame] atIndex:0];
code for addGradientToNavigationBar is
+ (CAGradientLayer*)addGradientToNavigationBar:(CGRect)navRect {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = navRect;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] CGColor],
(id)[[XAppDelegate getColor] CGColor], nil];
return gradient;
}

I have the same problem.
I was tying to add gradient layer to navigation bar with the UIBarStyleBlackTranslucent style for glass effect. After searching the web and a lot of experiments I have not found the right solution for that problem. Finally I've came up with following work around:
CAGradientLayer *yourGradient = ...
// Need two additional layers to mimic UIBarStyleBlackTranslucent style
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 320, 44);
NSMutableArray *cgColors = [[NSMutableArray alloc] init];
[cgColors addObject:(id)[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.2] CGColor]];
[cgColors addObject:(id)[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.6] CGColor]];
[cgColors addObject:(id)[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.5] CGColor]];
gradient.colors = cgColors;
CALayer *layer = [CALayer layer];
_layer.frame = CGRectMake(0, 22, 320, 22);
_layer.backgroundColor =[[UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.2] CGColor];
// This is needed for navigation bar buttons
[rootController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
// Adding layers to the first sublayer of the navigation bar layer
[[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:yourGradient atIndex:0];
// After adding the gradient layer, the UIBarStyleBlackTranslucent style effect disappears
// from the bar (but not from the buttons)
// So the following is needed to mimic it
[[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:gradient atIndex:1];
[[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:layer atIndex:2];
you may also want to add some borders to your layer as the borders seems to disappear.

Related

adding shadow to bottom of UINavigationBar only

I have the following code:
self.navigationBar_.layer.shadowColor = [UIColor blackColor].CGColor;
self.navigationBar_.layer.shadowOpacity = 0.3f;
self.navigationBar_.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.navigationBar_.layer.shadowRadius = 3.0f;
self.navigationBar_.layer.masksToBounds = NO;
and I basically only want to add the border to the bottom only and not the entire rectangle. How do I do this? The code above will add a shadow to the left, right, top border also.
Instead of a layer shadow, you could just use a gradient. You could just use a transparent PNG gradient, but here is an example of how to do it programmatically:
UIView *topShadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.navigationBar.bounds.size.width, 10)];
CAGradientLayer *topShadow = [CAGradientLayer layer];
topShadow.frame = CGRectMake(0, 0, self.navigationBar.bounds.size.width, 10);
topShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.25f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
[topShadowView.layer insertSublayer:topShadow atIndex:0];
[self.view addSubview:topShadowView];

iPhone groupTableViewBackgroundColor alternative for iPad

I am building an universal navigation app on iOS 5.0.
Setting the view backgroundcolor as below looks good for iPhone
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
But for iPad, i found out that this does not work as expected.
Is there any alternative to get same background on iPad using some color pattern?
If so ,please point me to some code samples or post some.
Recently when working on an XCode project we had set the background colour to groupTableViewBackgroundColor, which on an iPhone produces a pinstriped background, and works well. This background is the same one as used on UITableViews.
When porting this to iPad, the UITableViews now have a solid grey colour, and groupTableViewBackgroundColor essenitially is the same as [UIColor clearColor].
You would think that grabbing the colour that the UITableView uses would work. I am afraid it does not, as it turns out it is a slight gradient, not so much as you would notice, but when you're changing views, you notice.
The way to fix this is to create a CAGradientLayer, and apply it to a UIView before anything else loads.
First off, you need to create a function that emulates the gradient, below is what I used:
-(CAGradientLayer *)groupGradientColour {
CAGradientLayer *layer = [CAGradientLayer layer];
layer.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:(0xE2 / 255.0)
green:(0xE5 / 255.0)
blue:(0xE9 / 255.0)
alpha:1.0] CGColor],
(id)[[UIColor colorWithRed:(0xD0 / 255.0)
green:(0xD2 / 255.0)
blue:(0xD7 / 255.0)
alpha:1.0] CGColor],
nil];
layer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
return layer;
}
Then call this in viewDidLoad:
UIView *background = [[UIView alloc] init];
background.frame = CGRectMake(
0,
0,
[[UIScreen mainScreen] applicationFrame].size.width,
[[UIScreen mainScreen] applicationFrame].size.height);
CAGradientLayer *gradient = [self groupGradientColour];
gradient.frame = background.bounds;
[background.layer insertSublayer:gradient atIndex:0];
[self.view addView:background atIndex:0];
Once you have done this, you will have a gradient the same style as a UITableView.

UITableViewCell doesn't pickup UITableViewCellSelectionStyleBlue when adding CAGradientLayer sublayer

I have a UITableViewCell that I've added a gradient to by using CAGradientLayer. This works fine, but the table cell doesn't turn blue when selected, even after setting it's selectionStyle to UITableViewCellSelectionStyleBlue. If I don't add the gradient layer, it works fine.
Is there a way to make these items work together?
Here's my code inside of cellForRowAtIndexPath:
//cell gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(10, 0, 300, 50);
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1] CGColor],
nil];
gradient.locations = [NSArray arrayWithObjects:
(id)[NSNumber numberWithFloat:0.00],
(id)[NSNumber numberWithFloat:0.50],
(id)[NSNumber numberWithFloat:0.51],
(id)[NSNumber numberWithFloat:1.0],
nil];
[cell.layer insertSublayer:gradient atIndex:0];
//bring back rounded corners by creating a masklayer
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:gradient.bounds byRoundingCorners:UIRectCornerBottomRight|UIRectCornerBottomLeft cornerRadii:CGSizeMake(8, 8)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = gradient.bounds;
maskLayer.path = maskPath.CGPath;
gradient.mask = maskLayer;
//add the cell shadow
cell.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.layer.shadowRadius = 3;
cell.layer.shadowOpacity = 0.5;
cell.layer.shadowOffset = CGSizeMake(0, 0);
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
It looks like you might be putting the gradient over the view that changes when it is selected- the UITableViewCellSelectionStyleBlue view may be appearing, but you just cannot see it (a way to test it out is only have the gradient cover part of your cell- if the other part changes color when it is selected, then this is the issue).
If this is the issue, then you could do your own custom drawing for the cell when it is selected by subclassing UITableViewCell, or you could have the gradient disappear whenever the cell is selected (and reappear when the cell is deselected).

CAGradientLayer and scrollViewTexturedBackgroundColor

I'm trying to use CAGradientLayer to fade foreground controls against the background textured view.
So in a nutshell, I have a view with the bg color as scrollViewTexturedBackgroundColor. I have a view on top of that, whose contents I'd like to fade at the border into the background view's color.
CAGradientLayer* gradient = [CAGradientLayer layer];
gradient.frame = self->scrollableCanvas.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor scrollViewTexturedBackgroundColor] CGColor], (id)[[UIColor scrollViewTexturedBackgroundColor] CGColor], nil];
[gradient setStartPoint:CGPointMake(0.90, 1)];
[gradient setEndPoint:CGPointMake(1, 1)];
[[self->scrollableCanvas layer] addSublayer:gradient];
[[self->scrollableCanvas layer] setMasksToBounds:YES];
Unfortunately, CAGradientLayer doesn't seem to support UIColors as pattern images.
any ideas how I can achieve a simple border fade effect for the subview?
thanks
You can use a little trick:
//create normal UIImageView
UIImageView* iv = [[[UIImageView alloc] initWithImage: [UIImage imageNamed :#"bg_png.png"]] autorelease];
[superview addSubview: iv];
//draw gradient on top
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 1.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 1.0 alpha: 1.0] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];
[superview addSubview: view];
I made a category on UILabel that adds gradient at the end of the label using Max's answer.
If you need, here it is:
#import "UILabel+GradientEnding.h"
#implementation UILabel (GradientEnding)
- (void)addGradientEnding
{
//draw gradient on top
UIView *gradientAlphaView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - 80.0, 0, 80.0, self.frame.size.height)];
UIColor *startColor = RGBA(0xf7f7f7, 0);
UIColor *endColor = RGBA(0xf7f7f7, 1);
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = gradientAlphaView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(1.0, 0.5);
[gradientAlphaView.layer insertSublayer:gradient atIndex:0];
[self addSubview:gradientAlphaView];
}
#end
But you should use your own colors. And use this define:
#define RGBA(rgbValue, opacity) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:opacity]

CALayer w/ Animation as UIViews layer

I have a UIView & I want that view's layer to be a continuously looping animation but I'm having some trouble getting started. I have the following in my view subclass:
+ (Class)layerClass {
return [CALayer class];
}
Then in my viewController I have:
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],nil]];
[[[self view] layer] addSublayer:gradient];
This crashes my app. What exactly am I doing wrong? The app crashes w/ EXC_BAD_ACCESS.
Actually the crash you are having isn't related to not setting locations. The issue has to do with the fact that you're passing the wrong types in your setColors array. A CGColorRef is the types that is expected. Your code should look like this:
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],
(id)[[UIColor blueColor] CGColor],nil]];
[[[self view] layer] addSublayer:gradient];
The locations parameter of a CAGradientLayer is optional.
Best regards.
You forget to specify the locations for colors. By default, the location is nil.
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:
[[UIColor redColor] CGColor],
[[UIColor blueColor] CGColor],
nil]];
[gradient setLocations:
[NSNumber numberWithFloat:0.0], // for redColor
[NSNumber numberWithFloat:1.0], // for blueColor
nil]];
[[[self view] layer] addSublayer:gradient];