Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView background as tiled image

I have an image which height is about 1,5 screen on iPhone. I want to make it as a continuous background of UITableView. It have to scroll with the table. Is there any way to do it?

like image 615
pro_metedor Avatar asked Mar 16 '26 09:03

pro_metedor


2 Answers

You could turn this image into a pattern that can then be repeated indefinitely as the background of the table view. First, do this:

UIColor *backgroundPatternColor = [UIColor colorWithPatternImage:myImage];

Then set the background color of your table view, or any other view for that matter, to backgroundPatternColor.

like image 64
Richard Altenburg - Brainchild Avatar answered Mar 18 '26 23:03

Richard Altenburg - Brainchild


There is simple solution.

Use this code to set background pattern image for table view (pattern image may be small image, which you would like to tile):

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back"]];
}

Tiled image will be tiled infinite both up and down and scrolled with tableView.

Then do next important trick - set transparent background color to cells, because setting tableView backgroundColor also sets tiled backgrounds to cells:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor clearColor];
}

Use this approach for simple tableView cells, otherwise transparent backgrounds of complex cells yields performance lack.

like image 24
FunkyKat Avatar answered Mar 19 '26 01:03

FunkyKat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!