Changes the background color and background image of UITableView in the iOS application
- 2020-05-30 21:06:30
- OfStack
Change the header and footer background colors of UITableView
Changing the header and footer background colors of UITableView is a very common problem. The first rule I knew was to do this by implementing tableView: viewForHeaderInSection: return a custom View with nothing in it but the background color. But today I found a simpler way to do it:
For iOS 6 and beyond, the new delegate function can be implemented:
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
view.tintColor = [UIColor clearColor];
}
You can also change the color of the text:
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view;
[footer.textLabel setTextColor:[UIColor whiteColor]];
}
Modify the background image of tableView
Modify the background image of UITableView
1. The image is displayed in 'PatternImage' mode.
// viewDidLoad
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundImage"]];
// cellForRowAtIndexPath
cell.backgroundColor = [UIColor clearColor];
In this case the background picture is tiled like a floor tile. Pull the tableView background image and it will move. If the number of lines exceeds the height of the background image, the next image will be displayed.
2. Normal background picture.
// viewDidLoad
self.tableView.backgroundColor= [UIColor clearColor];
UIImageView*imageView = [[UIImageView alloc]initWithImage:[UIImageimage Named:@"BackgroundImage"]];
self.tableView.backgroundView = imageView;
// cellForRowAtIndexPath
cell.backgroundColor = [UIColor clearColor];
In this case the background image does not move, that is, no matter how many rows you see the same background.