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;
Related
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];
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];
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;.
I want to have a background image behind my UITableView. This works fine, but for each grouped UITableView it seems to be using my background image. I just want it to use the background image once:
Note: This is all inside of a UITableViewController class
self.view.backgroundColor = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.png"]]autorelease];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
headerView.backgroundColor = [UIColor clearColor];
footerView.backgroundColor = [UIColor clearColor];
You need to do two things:
Set background view (color) not in the UITableViewController, but in his parent (UINavigationController most likely, right?)
self.parentViewController.view.backgroundColor = [UIColor redColor]; // or whatever your image is
Set clear color for tableView background color (and optionally for its separatorColor)
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor clearColor];
Is there any way to set a background view on a UITableViewController?
I try with the code I am using on a UIViewController, but the view comes over all the contents of the table view. If I add the background view in the cellForRowAtIndexPath-method, it is not showing at all. Has anyone done this before or have an idea on how it can be done?
Here is the code I am using:
UIImage *image = [UIImage imageNamed: #"background.jpg"];
UIImageView *backImage = [[UIImageView alloc] initWithImage: image];
[self.view addSubview: backImage];
[self.view sendSubviewToBack: backImage];
I know it's been a long time, but just for the record..
I Think I found a better solution using UITableView.backgroundView:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"lisbon.png"]];
self.tableView.backgroundView = imageView;
[imageView release];
I tried with an image size of 320x480 on iPhone, and works perfect (I have tried with .jpg also).
(This is basically the same as Hans Espen's solution above, but uses convenience methods for brevity)
Put this in your -[UITableViewControllerSubclass viewDidLoad] method:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BackgroundPattern.png"]];
There's no point in avoiding a couple of autoreleases in a viewDidLoad method, since it only gets called rarely (when the view actually loads) and will therefore have negligible impact on performance.
N.B. You should always use PNG images on the iPhone, not JPEG or any other format.
Actually, I got it working! :)
NSString *backgroundPath = [[NSBundle mainBundle] pathForResource:#"background" ofType:#"jpg"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundPath];
UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];
self.tableView.backgroundColor = backgroundColor;
[backgroundColor release];
For Swift use this,
self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage.png"]];
In C# for a static UITableViewController with sections you can use:
using System;
using Foundation;
using UIKit;
using CoreGraphics;
using CoreAnimation;
namespace MyNamespace
{
public class CustomTableViewController : UITableViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
SetGradientBackgound();
}
private void SetGradientBackgound()
{
CGColor[] colors = new CGColor[] {
UIColor.Purple.CGColor,
UIColor.Red.CGColor,
};
CAGradientLayer gradientLayer = new CAGradientLayer();
gradientLayer.Frame = this.View.Bounds;
gradientLayer.Colors = colors;
gradientLayer.StartPoint = new CGPoint(0.0, 0.0);
gradientLayer.EndPoint = new CGPoint(1.0, 1.0);
UIView bgView = new UIView()
{
Frame = this.View.Frame
};
bgView.Layer.InsertSublayer(gradientLayer, 0);
UITableView view = (UITableView)this.View;
view.BackgroundColor = UIColor.Clear;
view.BackgroundView = bgView;
}
// Setting cells background transparent
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = base.GetCell(tableView, indexPath);
cell.BackgroundColor = UIColor.Clear;
return cell;
}
// Setting sections background transparent
public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
if (headerView.GetType() == typeof(UITableViewHeaderFooterView))
{
UITableViewHeaderFooterView hView = (UITableViewHeaderFooterView)headerView;
hView.ContentView.BackgroundColor = UIColor.Clear;
hView.BackgroundView.BackgroundColor = UIColor.Clear;
}
}
}
}
The result can be something like this: