UIView Background Image Transparency Problem - iphone

I added a subview with an PNG image that should display with transparency, but I am getting all black where the transparency should be.
The code is:
- (void)viewDidLoad
{
[super viewDidLoad];
toolbar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BannerBackground.png"]];
logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Logo.png"]];
[self.view addSubview:logoImage];
}
This is code for a iPad, iOS version 4.3. I have the same problem with both the simulator and the iPad.
Since this is the last view added, I expect it to have a z index higher than all of the other views, so the other views should show through the transparent areas. This is not working.

I noticed this behaves differently on iOS 4.3.x than 5.0.x. In 4.3.x I had to set opaque to YES, then set the background image, then set it back to NO.

Set your view's property opaque to no. like this [view setOpaque:NO];

Failing setOpaque:NO... check to see if your image is correctly exported and not corrupted etc.. that happened to me once

Create a UIImageView with transparent backgroundColor and your logo as its image, and add that:
UIImageView *logoView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"Logo.png"]];
logoView.backgroundColor = [UIColor clearColor];
[self.view addSubview:logoView];

This should work.
#import <QuartzCore/QuartzCore.h>
logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Logo.png"]];
[logoImage.layer setOpaque:NO];
logoImage.opaque = NO;
[self.view addSubview:logoImage];

Old question, but: any chance that your transparency is, for instance, black and not actually transparent? Not that I just did that or anything.
(When drawing the toolbar icon, iOS fills in any pixels, including black ones, with white. Make sure there's actually nothing where you expect your transparent area to be.)

Related

UITableViewController background image stretched

I'm trying to set the background image for a UITableViewController. I've got the following code which does set the background fine, but the image itself is being stretched to about twice the usual size.
[[self view] setAutoresizesSubviews:NO];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Default.png"]];
self.tableView.backgroundView = backgroundView;
self.tableView.backgroundColor = [UIColor clearColor];
[backgroundView release];
The image is only 640x960 pixels.
I've tried manually setting the frame of the backgroundView but it doesn't change the size of the background image.
Can anyone explain where I'm going wrong?
Many thanks.
In my expirience, you should avoid using
[UIColor colorWithPatternImage: ... ];
as it leads to some problems with picking the right image for Retina/Low-Res devices.
Try something more like this:
UIImageView* bgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"Default.png"]] autorelease];
[tableView.backgroundView addSubview:bgView];
Also make sure you have two PNGs:
Default.png - 320 x 480
Default#2x.png = 640 x 960

How to set UITableView background to image?

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

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...

iPhone: Removing rounded corners from table view cell

i have a table view in my application which shows rounded corners by default.
Is there any way to remove the rounded corners.
I have #import <QuartzCore/QuartzCore.h> in place and included the frame work as well.
eve the following code does not work
self.clipsToBounds = NO;
self.table.layer.cornerRadius = 0.0;
can any one suggest a solution?
Thanks in advance
For a UITableViewStyleGrouped cell, you'll need to change the backgroundView on your cell.
UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
bg.backgroundColor = [UIColor whiteColor];
cell.backgroundView = bg;
[bg release]; // if you're not using ARC
Are you using UITableViewStyleGrouped on your table view? If so change the style to UITableViewStylePlain and the cells will be rectangulars without rounded corners.

iPhone Fixed-Position UITableView Background

I'm building an iPhone app without the use of Interface Builder. I have set the background of a grouped UITableView in the following manor:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"groupedBackground.png"]];
I'm trying to fix this background image so that it doesn't scroll with the table cells. Does anyone know how to do this?
We find that it works exactly as you ask, if instead of using backgroundColor, you assign to backgroundView, like so:
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"background.png"]];
The table cells then scroll on top of the background, which is stationary. (This has been available since version 3.2 of the SDK, I believe.)
You can achieve a stationary background using a pattern image as follows:
UIView *patternView = [[UIView alloc] initWithFrame:tableView.frame];
patternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"groupedBackground.png"]];
patternView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.backgroundView = patternView;
A little late but maybe for all the others looking for a solution. I could get that work by calling the parentViewController 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.
You can place an additional view below UITableView and set its background, so if UITableView is transparent - you'll achieve your goal - it will have correct background and it will not scroll.
use this below code in swift 3.0 to achieve constant background for tableview
self.tableView.backgroundView = UIImageView(image: UIImage(named: "background.png")!)