I would like to change the color of an image view when the user touches it. I don't need it to change a great deal. However, I would like it to change to a shade of blue. I found the following code to change the gradient but it is not having any effect. I don't need a gradient, actually, only a tint. Would appreciate any suggestions.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = #[(id)[UIColor redColor].CGColor, (id)[UIColor blackColor].CGColor];
[snapshot.layer insertSublayer:gradient atIndex:0];
An easy and non-intrusive way is to add a translucent overlay to your image view. This can then be shown and hidden as the button is pressed and released.
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.bounds];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.3]; // Your color overlay goes here
[imageView addSubview:overlay];
}
overlay.hidden = !on;
}
You can try giving the UIImage a tint
UIImage *image = [[UIImage imageNamed:#"Your Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tintColor = [UIColor clearColor]; // Or any color really you'd like would work
imageView.image = newImage;
And then changing that tint when the user touches the image using a tap gesture recognizer
Related
How to set a gradient color to UITextField cursor? The effect is just like the picture below:
here is the picture
you can ask disigner for an one pix picture ,then do like as follows:
UIColor *collor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"one"]];
self.textfield.tintColor = collor;
To change UITextField Cursor color, you have set it as Tint Color of Type UIColor.
textField.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"gradient"]];
Do this for all text fields in your app using the UITextField appearance proxy:
[[UITextField appearance] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"gradient"]]];
BONUS:
If you need to set gradient color for UIView:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = #[(id)[UIColor whiteColor].CGColor, (id)[UIColor blackColor].CGColor];
[view.layer insertSublayer:gradient atIndex:0];
I have TableViewController with many cells. And background of my TableView Default White. I want to become some background image. I see this code :
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BackgroundPattern.png"]];
but i can`t use this on swift. So help me recycle this code to swift code or give your solution to this problem
Try this code :
self.tableView.backgroundColor = UIColor(patternImage: UIImage(named: "BackgroundPattern.png")!)
You need to set the background colour of each cell to clear colour.
In your cellForRowAtIndexpath method, add: cell.backgroundColor = [UIColor clearColor]
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:YOUR_IMAGE]];
self.tableView.backgroundColor = background;
or
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:YOUR_IMAGE]];
[imgView setFrame:self.tableView.frame];
self.tableView.backgroundView = imgView;
I want to build a totally transparent view, the backgroundColor maybe clearColor. Inside this view I want to put a small image. And in the four sides of this transparent view, I want to see the effect of shadow. The shadow must be totally outside the view. I know that I must override the drawRect method in UIView, but don't know how to do this.
try this....
UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(50.0, 100.0, 100.0, 100.0)];
shadowView.layer.cornerRadius = 15.0;
CALayer *layer = shadowView.layer;
layer.shadowOpacity = 1.0;
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowOffset = CGSizeMake(0,0);
layer.shadowRadius = 15;
[self.view addSubview:shadowView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)];
[imageView setImage:[UIImage imageNamed:#"icon.png"]];
[shadowView addSubview:imageView];
What is the problem with doing something like this?
#import <QuartzCore/QuartzCore.h> // at the top of the file
UIView *transparentView = [[UIView alloc] initWithFrame:CGRectMake(50,50,200,200)];
transparentView.backgroundColor = [UIColor greenColor];
transparentView.alpha = 0.5f;
transparentView.clipsToBounds = NO;
transparentView.layer.shadowColor = [UIColor blackColor].CGColor;
transparentView.layer.shadowOpacity = 1;
transparentView.layer.shadowRadius = 1;
UIImageView *imageView = [[]UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
imageView.image = [UIImage imageNamed:#"IMAGE NAME"];
[transparentView addSubview:imageView];
As you can see, based on your description, no drawRect: required, unless for some reason you want to draw the shadow manually, but there isn't a reason to.
In my application, I have UITableViewController, with table on it.
I filling background with image in this way:
UIImage* backgroundImage;
backgroundImage = [UIImage imageNamed:#"bg.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
But, background moves with all other view controller components, while user scroll the table.
Is it possible, to make immovable background, like HTML background?
Thanks.
Try
UIColor *color= [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"Background.png"]] autorelease];
self.tableView.backgroundColor = color;
or
UIImageView *imageView= [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Background.png"]] autorelease];
imageView.frame = self.tableView.frame
self.tableView.backgroundView = imageView;
I am having some trouble setting the correct location of the image for a UIImageView, which I am adding as a subview to a UIButton. I start by creating the button and adding some sublayers:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:bounds];
//add gradient background
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1.0].CGColor, (id)[[UIColor blackColor] CGColor], nil];
[button.layer insertSublayer:gradient atIndex:0];
//set green background
CALayer *layer = [CALayer layer];
layer.frame = CGRectInset(button.bounds, 5, 5);
layer.backgroundColor = [UIColor colorWithRed:.55 green:.8 blue:.5 alpha:1.0].CGColor;
[button.layer insertSublayer:layer above:gradient];
I then use two methods to set the image. The first (which I use if no image is saved) uses an image resource that is scaled to be the right size, and is part of the app bundle. I set this with the following code:
UIImageView *v = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"profile.png"]];
[v setBounds:CGRectInset(button.bounds, 8, 8)];
[button addSubview:v];
The trouble comes from when I use an image taken through the camera, and saved to the file system. I am setting this to the background using the following code:
//path is an NSString set the the documents location of the image.
UIImageView *v = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
[v setBounds:CGRectInset(button.bounds, 8, 8)];
[v setContentMode:UIViewContentModeScaleToFill];
[button addSubview:v];
The output to the screen now shows the image way offset from where it should be:
What am I doing wrong? How do I correctly scale/fit the image in its parent view? I have tried simply setting the button image, but nothing appeared (perhaps the image was behind the sublayers?).
Use -setFrame method to set the frame of imageview instead of bounds
[v setFrame:CGRectInset(button.bounds, 8, 8)];
Thus the code become
UIImageView *v = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
[v setFrame:CGRectInset(button.bounds, 8, 8)];
[v setContentMode:UIViewContentModeScaleToFill];
[button addSubview:v];