Reputation: 19303
I've searched online and on stackoverflow but couldn't find one good answer how to do it. I've read about
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section`
but there's nothing there that can help me.
The closest thing to an image background for the header there is:
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myBackground.png"]];
but it shows the picture as a pattern with no way controlling its size.
Upvotes: 3
Views: 8424
Reputation: 453
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
headerView.backgroundColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
headerView.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
headerView.layer.borderWidth = 1.0;
UILabel* headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,headerView.frame.size.height-40, tableView.frame.size.width - 5, 30)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:22.0];
headerLabel.text = @"yourText";
headerLabel.textAlignment = NSTextAlignmentLeft;
UIImageView *headerImage = [[UIImageView alloc]initWithFrame:headerView.frame];
headerImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"image.jpg"]];
headerImage.contentMode = UIViewContentModeScaleAspectFill;
[headerImage setClipsToBounds:YES];
[headerView addSubview:headerImage];
[headerView addSubview:headerLabel];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}
Upvotes: 0
Reputation: 371
Sure you can. In the method -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
you will need to simply instantiate a UIImageView
with an image, set the frame for the imageView object and return the imageView object.
Upvotes: 0
Reputation: 107141
If you want to set the image as section header use:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *myImage = [UIImage imageNamed:@"loginHeader.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,300,100);
return imageView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
Upvotes: 9
Reputation: 676
You can set the background colour to a pattern with image:
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]
Upvotes: 2