UITableViewController background image stretched - iphone

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

Related

Make a rectangle equal to image view

I have an imageView. I need to restrict the size of imageView to the white background imageOnly.The image is tightly bounded between right,left and top margin. I am confused on how to do at bottom, such that the image must fit in the white background only. How do I do that ?
http://i.stack.imgur.com/ztvQe.png
The code I am using
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 240,175,205)];
Frame width and height is 205*205
You can add your imageView as a subview of the view with the white background.
UIView *whiteBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(25,240,205,205)];
CGSize whiteSize = whiteBackgroundView.frame.size;
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,size.width,size.height)];
[whiteBackgroundView addSubview:_imageView];
In the example your imageView will completely fit the size of the white background view.
Try this
UIView *productView = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
_imageView.clipsToBounds =YES;
[productView addSubview:_imageView];
[self.view addSubview:ImageBackview];
just create one view and add this image in it, for example use bellow code..
UIView *ImageBackview = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
[ImageBackview addSubview:_imageView];
[self.view addSubview:ImageBackview];

Translucent UITableViewCell has no borders

I created a UITableView. I gave it a background image like this:
UIImageView *imageview1 = [[UIImageView alloc] init];
imageview1.image = [UIImage imageNamed:#"blood2.gif"];
evc.tableView.backgroundView = imageview1;
And then I made the cells translucent by changing them in the tableView:cellForRowAtIndexPath: method:
UIView *backgrView = [[UIView alloc] initWithFrame:CGRectZero];
backgrView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
cell.backgroundView = backgrView;
But now there are no borders around the UITableViewCells anymore. I already set the separatorColor to black but the borders won't show up...
Check with this
[cell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[cell.contentView.layer setBorderWidth:2.0f];

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

UIView Background Image Transparency Problem

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

UIImageView Background Image and Rotation Help

In my iPad app I currently have a background image png of 1024x1024 pixels and set it using the following
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background-image"]];
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];
This adds the UIImageView fine and the view is centered in landscape however it is off center in portrait.
I have played around with some of the contentMode settings but not had any luck, I either get portrait to be center or landscape to be center but not both.
Could someone please help me with getting the UIImageView centered in both portrait and landscape after rotation.
Thanks in advance
Matt
You need to set the contentMode, the frame and an appropriate autoresizing mask:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background-image"]];
imgView.frame = self.view.bounds;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
imgView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];
Check the autoresizingMask property for the imageView.