Try to have a UIImage in a MKAnnotation - iphone

I try to have a UIImage in a MKAnnotation:
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
leftCAV.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"..."]];
pin.leftCalloutAccessoryView = leftCAV;
The code is run, but I don't have a transparent background
See that:
http://img19.imageshack.us/f/1234wr.png/
Have an idea?
thanks :)

Any reason not just using an imageView?
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Location.png"]];
annotationView.leftCalloutAccessoryView = iconView;

Did you try:
leftCAV.backgroundColor = [UIColor clearColor];
?
I'll assume your image is 32x32. Does it have a transparent background? (I could not tell if what I was looking at at the link you provided was what you intended. There's too much going on on that site! :-)

Add this leftCAV.opaque=NO;.

Related

Set background image in table view when story boarding is also used

I am doing the following to set the background image:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"appback_new.png"]];
[self.tableView setBackgroundView:imageView];
in view did load method. Also, I made sure that the background color is set to clear color in story board.
But when the view loads, it loads with a black color.
Did any one face this issue and knows how to resolve it?
Thanks.
Try this it should work :
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"appback_new.png"]];
self.tableView.backgroundColor = background;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"appback_new.png"]];
try your code after inserting this line of code
tableView.backgroundColor = [UIColor clearColor];

What is the correct way of adding a background image to a UIView?

Did read on couple of forums that adding a background image with "colorWithPatternImage" will consume more memory than usual.
The bad way:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Better solution:
UIImageView* iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]]; iv.userInteractionEnabled = YES;
self.view = iv;
[iv release];
I have two question regarding this! which solution is better ? And why ?
I am also trying to figure out how to place my labels on the top of the imageView.
UIColor* tmpColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:#"background.png"]];
self.view.backgroundColor=tmpColor;
[tmpColor release];

UITableView background Image

I'am trying to setup a png image as my tableview's background. With the following code all are fine! But only on iPhone Simulator. If I try to run the application on an iPhone device the background of tableview remains white (or clear). Do you thing thing that is something about the way I tried to set the background color. I have been trying many ways until the following, but all have the same issue.
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TableViewBackground.png"]];
Thank you in advance!
Please use following code.
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
[tempImageView release];
Thanks,
I think this approach cleaner:
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];
Simple swift version will be
let tempImageView = UIImageView(image: UIImage(named: "yourImage"))
tempImageView.frame = self.tableView.frame
self.tableView.backgroundView = tempImageView;
Is your image actually named TableViewBackground.PNG (note the capitals)? You need to have the case match exactly on an iOS device, whereas it doesn't need to match exactly in the Simulator.
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
[tempImageView release];
This works perfect. Thanks to Ravin by Jona
An IBDesignable tableview subclass that will let you attach an image from interface builder
import UIKit
#IBDesignable
class DesignableTableView: UITableView {
#IBInspectable var backgroundImage: UIImage? {
didSet {
if let image = backgroundImage {
let backgroundImage = UIImageView(image: image)
backgroundImage.contentMode = UIViewContentMode.ScaleToFill
backgroundImage.clipsToBounds = false
self.backgroundView = backgroundImage
}
}
}
}
[TableView setBackgroundView:nil];
[TableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"apple.png"]] ];
This works perfectly fine, also if you want to use a pattern image for your background.
UIImage *image = [UIImage imageNamed:#"something.png"];
self.tableView.backgroundView = nil;
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
A little late but maybe for all the others looking for a solution. I could get that work by using the following code in the viewDidLoad method of the UITableViewController:
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
or with using just a background color:
self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.tableView.backgroundColor = [UIColor clearColor];
It took me a while to figure that out but now it works like a charm.
Objective-c version:
cell.backgroundColor = [UIColor clearColor];
Also see this:
How to create a UITableViewCell with a transparent background
Thanks #Ravin for your answer.
In Swift 5.0:
let tempImageView = UIImageView(image: UIImage(named: "justin_bieber_topless.png"))
tempImageView.frame = self.tableView.frame
self.tableView.backgroundView = tempImageView;
UIImageView *imageviewTemp = [[UIImageView alloc] init];
[(UIImageView *)imageviewTemp sd_setImageWithURL:[NSURL URLWithString:self.stringOfPassedImage]];
[imageviewTemp setFrame:self.tableView.frame];
self.tableView.backgroundView = imageviewTemp;

Adding backgroundimage to my app

I am using this code to add a background image to my tableview
myTable.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]] autorelease];
on the internet they say this code works, but it's not working for me
can someone help me please?
if its a UITableViewController, as I believe it is, do this:
[myTable setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
or you can do (on a non-table view controller)
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
or on viewDidLoad you can do:
UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]] autorelease];
[self.view addSubview:backgroundView];
but thats a bit messy - I prefer to use setBackgroundColor:
The problem is that backgroundview for tableview is a UIView, not a UIImage. So you need to create a view, easiest way is to use a UIImageView. For example this would put your splash screen image as the background view for the table in a UITableViewController...
UIImage *backImage = [UIImage imageNamed:#"default.png"];
UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage];
self.tableView.backgroundView = backImageView;
The methods mentioned above are pre the introduction of tableview property backgroundview in 3.2
edit - dear god I was obviously having a coffee deficit today :) The op is indeed using a UIImageView! The only thing I can think is that he is targeting a pre 3.2 platform...

setting view's background image in iphone

Can I set an image for the view's own background image? I don't want to use UIImageView control. It seems not to be possible using IB. Do I have to do it programatically?
Thank you.
What a waste of resources, use :
[[self view] setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed:#"background.png"]]];
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"plain_bg.png"]];
What's wrong with UIImageView? This is a perfect use for it.
I really don't think that using backgroundColor in this way is the proper way to do things. Personally, I would create a UIImageView and insert it into your view hierarchy.
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"pic.png"]];
self.view.backgroundColor = background;
Try this
Its working for me