开发者

iPhone Fixed-Position UITableView Background

开发者 https://www.devze.com 2022-12-15 00:13 出处:网络
I\'m building an iPhone app without the use of Interface Buil开发者_运维技巧der. I have set the background of a grouped UITableView in the following manor:

I'm building an iPhone app without the use of Interface Buil开发者_运维技巧der. 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")!)
0

精彩评论

暂无评论...
验证码 换一张
取 消