I am subclassing a UITableViewCell and in the init function I am doing the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"packagelistcell_background.png"]];
[imageView setFrame:CGRectMake(0, 0, 300, 81)];
self.backgroundView = imageView;
[imageView release];
When that displays on the screen, the image appears slightly blurred. For testing, I placed a straight UIImageView above the table view and the image looks perfect and crisp. The table view is a grouped table view. Do you know why it is blurring the image?
I think the cell has been resized and so is the backgroundView.
Try
imageView.autoresizingMask = UIViewAutoresizingNone;
Try shifting the imageView by 0.5 pixels.
Unfortunately #freytag's solution didn't work for me.
To solve this problem I had to add the UIImageView to a UIView container and set this last as background view of my Custom Cell instead of setting directly the UIImageView:
UIImageView *imgView = [[UIImageView alloc] initWithImage:#"cellBackground.png"];
UIView *backgrContainer = [[UIView alloc] initWithFrame:self.backgroundView.frame];
[backgrContainer addSubview:imgView];
[self setBackgroundView:backgrContainer];// instead of [self setBackgroundView:imgView];
[backgrContainer release];
[imgView release];
For some reason setting directly the UIImageView makes it blurry (even if no resize ocurrs). Setting autoresizingMask = None or contentMode <> ScaleToFill for the UIImageView did not fix my problem.
Also beware of the UITableView style, since using a Grouped Style will result in less width for the cell's background view, and this can also resize the image.
( Retina device/simulator doesn't seem to have this problems about blurry image in the cell backgroundVIew)
Related
After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think
I want to have an image behind my tableview, i added a UIImage behind and set the tableview background to clear, in IB it shows the image behind the tableview but when i run it i get a black background, anybody can help me with this?
take off the image and add this code to your viewDidLoad:
UIView *patternView = [[UIView alloc] initWithFrame:self.tableView.frame];
patternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
patternView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.backgroundView = patternView;
this is the way to add a fixed image as a background of a tableview.
or you could do that without subclassing by simply assigning the tableview's backgroundView property in your main controller class...
Try following code in -(void)viewDidLoad method
self.tblView= [[UITableView alloc] initWithFrame:CGRectMake(17,110, 280.0, 265) style:UITableViewStyleGrouped];
self.tblView.delegate=self;
self.tblView.dataSource=self;
//self.tblView.backgroundColor=[UIColor clearColor];
self.tblView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img3.png"]];
[self.view addSubview:self.tblView];
I'm trying to add a back ground image to UITextView. The image is just a small border which will be placed at the top of text view. (the image resembles teared paper image). I'm using following code
UIImageView *imgView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 320, 13)];
imgView.image = [UIImage imageNamed: #"teared_paper.png"];
[tView addSubview: imgView];
[tView sendSubviewToBack: imgView];
[imgView release];
My text view's height is 150 pixels only. (text view occupies only small portion of view and it is at the top of the view so that it will appear to the user when keyboard is there)
The problem is that, when I add more lines of text, text view is scrolling automatically. And at the same time, the background image that I added is also scrolling. How can I prevent the background image to stay on the top all the time irrespective of scrolling.
Don't add the image view as a subview of the text view. Instead, have both the image view and the text view as children of the main view, position the image view behind the text view, and set the background color of the text view to transparent with:
[tView setBackgroundColor:[UIColor clearColor]];
Can you try putting a UIImageView behind the UITextView and making the UITextView transparent?
Try it with:
UIImageView *imgView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 320, 13)];
imgView.image = [UIImage imageNamed: #"teared_paper.png"];
[tView addSubview: imgView];
[imgView release];
Just make textView transparent :
[textView setBackgroundColor:[UIColor clearColor]];
no need to bring the subview to front.
Hope this works for u..
I've noticed that a number of apps have a custom background image for UITableView (or have set the background to a color.)
I can't find any API in the UITableView class to affect this, and my attempts in interface builder have failed (trying to set the color of the UIView)
Any suggestions? I've seen a third party app with pictures, so I know it can be done.
I just ran into the same question myself, but I found a way to do it that TomSwift touched on. As he mentioned, UITableView has a backgroundView property that is intended to do just what you're looking for. If you want to set the background color, you should instead use the backgroundColor property, as others have said. So:
// Set an image as the background of a UITableView called 'tableView'
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyImage.png"]];
[tableView setBackgroundView:bg];
or:
// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];
add this line in you code where you want to set background color for your image
[YourView(or imageView) setbackgroundColor:[UIColor
groupTableViewBackgroundColor]];
Thanks
UIImageView *image = [[UIImageView alloc] initWithImage:
[UIImage imagenamed:#"no_image.png"]];
[self.view addSubview:image];
UITableView *tableview = [[UITableView alloc] init];
[image addSubview:tableview];
tableview.backgroundcolor = [UIColor clearColor];
try this. it works for me.
CALayer *background = [CALayer layer];
background.zPosition = -1;
background.frame = self.view.frame;
background.contents = [[UIImage imageNamed:#"background.jpg"] CGImage];
[tableView.layer addSublayer:background];
you need to add an image view and set the table background color to clear color see this link for tutorial
Set the backgroundView property of the tableView. You can set it to a view where you have set a custom backgroundColor, or you can set it to a UIImageView where you've set an image.
alternative solution of GhostRider
No coding
Make UiImageView by the interface builder,
put your image in resources.
set it in UIImageView Image property by the use of Inspector.
select table view and set it background color to clear(for this make opacity to 0 by using attribute inspector).
This is how I did it. Probably not the best way, but works well enough.
UIImage *bg = [UIImage imageNamed:#"bg"];
[bg drawInRect:self.view.window.frame];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.window.frame];
bgView.image = bg;
activityTable.backgroundView = bgView; //activityTable = UITableView
I have an UIImageView and a few buttons defined in interface builder. I want to display an image with the buttons on top of it. I add an image to the UIImageView programatically but it covers up the buttons. I tried using sendSubviewToBack and the image disappeared completely. Here's my code
UIImageView *imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(0.0, 0.0, 320.0, 480.0)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"mypic%d.png", index]];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
[imageView release];
Please help.
ok, i'm a newbie and I was dumb. I was defining a UIImageView on interface builder and also in the code thus hiding one behind another. sorry.
You might also choose to simply create the image view in Interface Builder, create an outlet to it, then set the image programmatically. Doesn't look like you need to dynamically mess with the view hierarchy here.