setting background in uitableview - ios

Well I am having a tableview and I want to set background.
I tried this:
self.mapMain.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"maptest.png"]];
and this:
NSString *backgroundPath = [[NSBundle mainBundle] pathForResource:#"maptest" ofType:#"png"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundPath];
UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];
self.mapMain.backgroundColor = backgroundColor;
[backgroundColor release];
but the problem is this: I can see the image but it is like it is repeated in every cell. And if i scroll after the list, I can see the rest of the image. What I want is the image to be static not repeated in every cell. So image is the height of the iphone screen and each cell height is 80.
I have also tried inserting an UIImageview in my view controller and then
did Edit>Arrange>send to Back but send to back is not clickable.

Try this in viewDidLoad
[self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"maptest"]]];

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

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

adding a background image turns the background all black - likely image not detected

I created a group called img and put an image there that I wanted to appear as the background.
But instead, the background turned all black.
Here is the code I used for this. I put the code in the viewDidAppear method
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"img/building.png"]];
any thought on what I might have done wrong?
Use [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"building.png"]]; instead
I am assuming you are loading an image from the application bundle? If this is the case, the UIImage is not being created correctly.
Try:
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"building" ofType:#"png"]];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];

How to set a background image to label

I want to display a number on image like badge of size 30*28, which is placed upon button image,
I have an badge image to set up on the top of button image..
on top of badge image I should able to display text or number and my badge size is 30*28.
so, for this to achieve I want to set a label on top of my button image and so I want to set label background to some image called badge image.
found this in stack overflow itself as the above code just gives the background without the label
was useful for me
so sharing
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blah"]];
See: Adding Background image to UILabel
You can't add a background image to a UILabel, but you can add a UIImageView as a subview to a UILabel. Make sure the UILabel has backgroundColor = [UIColor clearColor]; for transparency.
E.g.
UIImageView *labelBackground = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"mybg.png"]];
[myLabel addSubview:labelBackground];
[labelBackground release];
myLabel.backgroundColor = [UIColor clearColor];
Should work. Untested.
To make the image fit in your UIview, try this code
UIImage *backgroundImage = [UIImage imageNamed:#"imageNameHere"];
UIGraphicsBeginImageContext(self.labelName.frame.size);
[backgroundImage drawInRect:CGRectMake(0, 0, self.labelName.frame.size.width, self.labelName.frame.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.labelName.backgroundColor = [UIColor colorWithPatternImage:newImage];

How to resize image to fit UITableView cell?

How to fit UIImage into the cell of UITableView, UITableViewCell (?).
Do you addSubview to cell or is there a way to resize cell.image or the UIImage before it is assigned to cell.image ?
I want to keep the cell size default (whatever it is when you init with zero rectangle) and would like to add icon like pictures to each entry. Images are slightly bigger than the cell size (table row size).
I think the code looks like this (from top of my head):
UIImage * image = [[UIImage alloc] imageWithName:#"bart.jpg"];
cell = ... dequeue cell in UITableView data source (UITableViewController ...)
cell.text = #"bart";
cell.image = image;
What do I need to do to resize the image to fit the cell size?
I've seen something like:
UIImageView * iview = [[UIImageView alloc] initWithImage:image];
iview.frame = CGRectMake(...); // or something similar
[cell.contentView addSubview:iview]
The above will add image to cell and I can calculate the size to fit it, however:
I'm not sure if there is a better
way, isn't it too much overhead to
add UIImageView just to resize the
cell.image ?
Now my label (cell.text) needs to be
moved as it is obscured by image,
I've seen a solution where you just
add the text as a label:
Example:
UILabel * text = [[UILable alloc] init];
text.text = #"bart";
[cell.contentView addSubview:iview];
[cell.contentView addSubview:label];
// not sure if this will position the text next to the label
// edited original example had [cell addSubview:label], maybe that's the problem
Could someone point me in correct direction?
EDIT: Doh [cell.contentview addSubview:view] not [cell addSubview:view] maybe I'm supposed to look at this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = ...;
CGRect frame = cell.contentView.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
myLabel.text = ...;
[cell.contentView addSubview:myLabel];
[myLabel release];
}
This site posts code on rescaling a UIImage*. You could play with the scaling to get the right ratio for the UIImage* you are using, to scale it down.
If you have access to the apple devloper center, there is a video about scaling images for table views. It is more focused on performance issues and talks lots about threading, but he does shows some sample code used to resize images.
Video name: "Effective iPhone App Development - Part 2" about 30 mins in.

Resources