Change gradient for UIButton for different states - iphone

Recently I've encountered a problem. I have a UIButton and 2 images for it. 1 for normal state and 1 for highlighted. The problem is that normal state image is transparent and I should use gradient to fill it. But if I do it, my highlighted image never appears. Here's the part of the code:
UIImage *balloonImage = [[UIImage imageNamed:#"balloon.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
UIImage *balloonImageDown = [[UIImage imageNamed:#"balloon-down.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 320 - 45 - 10, 100);
gradient.cornerRadius = 3.0f;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:221.0f/255.0f green:231.0f/255.0f blue:246.0f/255.0f alpha:1.0f]CGColor], (id)[[UIColor colorWithRed:191.0f/255.0f green:207.0f/255.0f blue:234.0f/255.0f alpha:1.0f] CGColor], nil];
self.balloonButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.balloonButton.layer insertSublayer:gradient atIndex:0];
[self.balloonButton setBackgroundImage:balloonImage forState:UIControlStateNormal];
[self.balloonButton setBackgroundImage:balloonImageDown forState:UIControlStateHighlighted];
self.theImageView = [[[UIImageView alloc]init]autorelease];
[self.balloonButton addSubview:theImageView];
[self.contentView addSubview:balloonButton];
The gradient looks as it should and everything is working. Except for changing the image if the button is tapped.
Is there a way to put balloonImageDown in front of the gradient? Or how can I do that?
Any help would be appreciated.

[yourButton setTitleColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.9 alpha:0.5]
// set 'alpha' to something less than 1. -----^^^
This is what you do to make it transparent.(changing the alpha value)
You should be changing these values in touchdown and touch up method.

Related

How to set the background color of an UIButtonTypeRoundedRect Button?

I tried tintColor = [UIColor redColor] and it does the job, but it only show the expected color when the button is being pressed.
In Coloration of UIButtons of type UIButtonTypeRoundedRect they answer the same question but you need an image to implement it, is there any way to do it without an image?
Here is what i want: image
Here is what i'm getting using images: image
Remember... NO IMAGES!
well using an image is one way of doing it, but i guess you already clear that in your question that you do not wish to use an image. here is button that i created and colored programmatically.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
you can change the [UIColor ColorWithRed: wo whatever you want or even use [UIColor redColor] or any other things you want to do with this.
here is a screen shot:
adding a background color to rounded button is not possible, change the type of the button to custom and then use quartz core to set a round button. so add that to your project and import it in the .m and it should work. hope this works for you man.
to change the button to something similar to the image you showed change the frame to :
btn.frame = CGRectMake(50.0, 100.0, 200.0, 50.0);
and change the radius to :
btn.layer.cornerRadius = 7;
that should do it.
You can create a method to generate an UIImage from a UIColor and then set the returned image as the background of the button, for example:
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIImage *image = [self imageWithColor:[UIColor colorWithRed:151.0/255.0
green:36.0/255.0
blue:40.0/255.0
alpha:1.0]];
[self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.button setBackgroundImage:image forState:UIControlStateNormal];
self.button.layer.cornerRadius = 6.0;
self.button.layer.borderWidth = 1.0;
self.button.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.button.clipsToBounds = YES;
Using this I get
here's how I should do it:
download a button image from the internet or create an image yourself, to make it look like you want it to look like. Photoshop might be your best shot for this. make sure you have a transparent background and save it as a .png.
then add it to your xcode project. to make the button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonAction)
forControlEvents:UIControlEventTouchDown];
[btn setTitle:#"Use it!" forState:UIControlStateNormal];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btn.titleLabel.font = [UIFont systemFontOfSize:20];
btn.frame = CGRectMake(position and size of your button);
[self.view addSubview:btn];
yourImage.png needs to be replaced with the name of your image.
The buttonAction is the action your button performs when it's tapped. if you just add:
- (void) buttonAction
{
}
anywhere in your file, anything you added between those brackets will be performed.
Make sure the size of your button is the same as the size of your CGRect, to make sure it doesn't look stretched out or anything you don't want it to look like.
If you have any questions, feel free to ask them.
Hope this helped you

UINavigationBar with two rounded corners

I have seen this answer
https://stackoverflow.com/a/9243472/563381
And while it works well visual as soon as you set the mask on the Navigation bar's layer it no longer responds to touches... So the back button that appears on the bar can't be clicked. Any solution to cause touches to go through a CALAyer? I didnt think CALayer's blocked touches or that a mask would ever block touches.
Well, I really don't know why CALayer blocks touches and this sounds odd to me...
The way I round corners of UINavigationBar consists in putting 2 UIImageView (10x10 pixels) in the corners and add 2 images to them. These images work as a mask, without blocking touches. If you use antialiasing to draw your images, the look is perfect.
You should try to use this code:
self.navigationController.navigationBar.translucent = YES;
This wil turn on your back button.You can see your button, but its in another layer. That's why it will not work the touches..
UPDATE:
Use this line of code for test. This will work like a charm for you.
//Style UINavigationBar
UIView *background = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
background.backgroundColor = [UIColor blackColor];
[self.view addSubview:background];
self.navigationController.navigationBar.tintColor = [UIColor cyanColor];
self.navigationController.navigationBar.translucent = YES;
CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];
[capa setShouldRasterize:YES];
//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f; //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;
//Back Btn
UIButton *btnback = [UIButton buttonWithType:UIButtonTypeCustom];
[btnback setFrame:CGRectMake(0, 0, 54, 29)];
[btnback setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
UILabel * btnlabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 40, 23)];
btnlabel.backgroundColor = [UIColor clearColor];
btnlabel.textColor = [UIColor whiteColor];
btnlabel.font = [UIFont boldSystemFontOfSize:13];
btnlabel.text = #"back";
[btnback addSubview:btnlabel];
[btnback addTarget:self action:#selector(backHome:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnback];

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];

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).

UISegmentedControl tint color on touch

I have a UISegmentedControl in my app (see code below) :
// --------------- SETTING NAVIGATION BAR RIGHT BUTTONS
NSArray *segControlItems = [NSArray arrayWithObjects:[UIImage imageNamed:#"up.png"],[UIImage imageNamed:#"down.png"], nil];
segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.frame = CGRectMake(25.0, 7, 65.0, 30.0);
segControl.tintColor = [UIColor blackColor];
[segControl addTarget:self action:#selector(segAction:) forControlEvents:UIControlEventValueChanged];
if (current == 0) [segControl setEnabled:NO forSegmentAtIndex:0];
if (current == ([news count]-1)) [segControl setEnabled:NO forSegmentAtIndex:1];
// ---------------
But I can't make it to show something when you click on it ...
It functionnally works perfectly but I would like it to tint to gray when you click on it (but just when you click) ... would that be possible ?
Thank you,
Gotye.
Seems as if the selected segment's tint color is made darker than original tint. Therefore if your tint is black then the selected segment's tint color doesn't change since there isn't anything darker than that.
I've looked around and haven't found any nice way to control the selected segment's tint color for this control.
A recipe to create own segmented controls, which (I guess) can be configured as you want:
idevrecipes custom segmented control
BTW: A very very interesting website!
Try this. It works if you use segment titles, you may need to modify it if you use images.
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segControl addTarget:self action:#selector(changeColors:) forControlEvents:UIControlEventValueChanged];
-(IBAction)changeColors:(UISegmentedControl *)sc{
for (int i=0; i<sc.numberOfSegments; i++) {
// reset all the titles in the segmented control
[sc setTitle:[NSString stringWithFormat:#"%i", i] forSegmentAtIndex:i];
}
if (sc.selectedSegmentIndex < 0 || sc.selectedSegmentIndex >= sc.numberOfSegments) {
return;
}
CGFloat width = sc.frame.size.width / sc.numberOfSegments - 1;
CGRect rect = CGRectMake(0, 0, width, sc.frame.size.height - 2);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// background gradient
UIColor *colorOne = [UIColor colorWithWhite:0.6 alpha:1.0]; //top of gradient
UIColor *colorTwo = [UIColor colorWithWhite:0.4 alpha:1.0]; //bottom of gradient
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,rect.size.height), 0);
// black shadow at the top
colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0 alpha:0.3].CGColor, (id)[UIColor clearColor].CGColor, nil];
gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,2.0), 0);
UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, sc.frame.size.height - 4)];
title.text = [sc titleForSegmentAtIndex:sc.selectedSegmentIndex];
title.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0];
title.textAlignment = UITextAlignmentCenter;
title.textColor = [UIColor whiteColor];
title.backgroundColor = [UIColor clearColor];
title.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
title.shadowOffset = CGSizeMake(0, -0.5);
[title.layer renderInContext:ctx];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[sc setImage:img forSegmentAtIndex:sc.selectedSegmentIndex];
}