Prevent cell.backgroundView image from getting squeezed? - ios

I'm currently setting the backgroundView of my UITableViewCells to images being pulled from eBay's API.
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageWithData:imageData] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
[cell setNeedsLayout];
The problem is that the table tries to squeeze the image to fit the height of the cell as you can see below:
What I want it to do is to only show the middle section of the image in the cell, as you can see below. Is there any way to do this?

When you add the Imageview as a backgroung view of cell have to set the content mode of the image before setting to the cell background view:
Try the following code and let me know.
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imageData]];
imageView.contentMode = UIViewContentModeCenter;
imageView.clipsToBounds = YES;
cell.backgroundView = imageView;

Related

Background Image overlaps Refresh Control

In xcode 7.1, I have used the refresh control feature of a TableView.
I have also used the following code to set an image to the background
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
This image overlaps the activity indicator on the app so you can't see if its working. Is there a way to put the activity indicator back on top?
Thanks
Use this line
self.tableView.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:#"image.png"]];
Instead of using
self.tableView.backgroundView = tempImageView;
Note: but your UIImage size should be greater/equal to UITableView size

How can I insert an imageView behind a UITableView in a fixed position (no scrolling)?

I am trying to insert an image behind my table view using:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"newpink120"]];
imageView.frame=CGRectMake([[UIScreen mainScreen]bounds].size.width-60, [[UIScreen mainScreen]bounds].size.height-60-self.navigationController.navigationBar.frame.size.height, 60, 60);
[self.tableView addSubview:imageView];
[self.tableView sendSubviewToBack:imageView];
This seems to work but the image scrolls with the table. Is there anyway to keep it fixed in the bottom right corner?
For creating a static tableview background, you have to do 2 things
1) set self.tableView.backgroundColor = [UIColor clearColor];
2) and then initialise the table background view with an UIImageView and set the image you want to show there.
[self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]] ];
Your image scrolls because it has been added in the tableview content and when you scroll the tableview, all the content scrolls.
But you can add the image in the superview of your tableView, behind it.
For example, if the tableView is in self.view you can try :
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"newpink120"]];
imageView.frame=CGRectMake([[UIScreen mainScreen]bounds].size.width-60, [[UIScreen mainScreen]bounds].size.height-60-self.navigationController.navigationBar.frame.size.height, 60, 60);
[self.view addSubview:imageView];
[self.view bringSubviewToFront:self.tableView];
(Make sure that the tableView has a clear backgroundColor.)

Centering a UIImageView inside a UIScrollView

I am trying to center a UIImageView in a scroll view. Here is the code I tried with no results.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kReceiptDetailScrollViewBackgroundCornered]];
[iv setContentMode:UIViewContentModeCenter];
[[self scrollView] insertSubview:iv atIndex:0];
Anybody know what's wrong with this?
You set the image view's content mode to center, which means that the image is not scaled and it can be larger than the image view that displays it.
UIImage *image = [UIImage imageNamed: kReceiptDetailScrollViewBackgroundCornered];
UIImageView *iv = [[UIImageView alloc] initWithImage: image];
iv.contentMode = UIViewContentModeScaleAspectFit;
iv.frame = [self scrollView].bounds;
[[self scrollView] addSubview: iv];

UIImageView fails to show image

UIImage is not displaying an image. I've created a UIImageView custom class for table cells using:
imgView = [[UIImageView alloc]init];
frame= CGRectMake(boundsX+10 ,0, 50, 44);
imgView.frame = frame;
Then, I tried to load each cell and nothing. But, I am able to read the image url in the log messages.
NSURL *url = [NSURL URLWithString: [dict objectForKey:#"imageAdd"]];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage * image = [[UIImage alloc] initWithData:[[data copy] autorelease]];
NSLog (#"The image is located at the URL: %#", url);
cell.imgView.image = image;
return cell;
Standard UITableViewCell already contains UIImageView that appears to the left to all your labels if its image is set. You can access it using imageView property:
cell.imageView.image = someImage;
If for some reason standard behavior does not suit your needs (note that you can customize properties of that standard image view) then you can add your own UIImageView to the cell as Aman suggested in his answer. But in that approach you'll have to manage cell's layout yourself (e.g. make sure that cell labels do not overlap image). And do not add subviews to the cell directly - add them to cell's contentView:
// DO NOT!
[cell addSubview:imageView];
// DO:
[cell.contentView addSubview:imageView];
As far as I can see from your code, you never added imgView as a subview of your cell. add the line:
[cell.contentView addSubview:imgView];

setting an image in background of a label programmatically

I have a label (many labels in fact) now I want an image in the background of my label (same image) so that text writtenin my label is shown to user. How to do it programmatically?
The simplest way would just be to simply layer a UIImageView behind the UILabel. If you wanted to make it more robust you could make a UILabel subclass that exposes a method or property to set the image background and it would add the image to itself.
CGPoint labelPos = CGPointMake(123, 234);
UIImage *theImage = [UIImage imageNamed:#"button_bg.png"];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:theImage];
theImageView.frame = CGRectMake(labelPos.x, labelPos.y, theImage.size.width, theImage.size.height);
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelPos.x, labelPos.y, 100, 50)];
theLabel.backgroundColor = [UIColor clearColor];
// ...label config...
[self.view addSubview:theImageView];
[self.view addSubview:theLabel];
[theImageView release];
[theLabel release];

Resources