StephenAshley.developer
StephenAshley.developer

Reputation: 1064

How to set width of UITableView header view?

Is there any way to set the width of a UITableView section header view to something less than the full width of the UITableView? Now matter how wide I set the view returned by tableView:viewForHeaderInSection:, the header view is stretched to the width of the UITableView.

Here is my code:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)];
  label.backgroundColor = [UIColor yellowColor];
  label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  return label;
}

Upvotes: 2

Views: 10009

Answers (3)

Anuj Kumar Rai
Anuj Kumar Rai

Reputation: 666

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{
    int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];

   if([[self.groupHeadings objectAtIndex:aSection] isEqualToString:@""])

        return nil;

    return [self.groupHeadings objectAtIndex:aSection];

}

Upvotes: 2

Bala
Bala

Reputation: 2895

Try this ., if i have understood your question this will surely work for you

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 60)];
    headerView.backgroundColor = [UIColor clearColor];

    UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0,0, 50, 50)];
    label.backgroundColor = [UIColor yellowColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

    [headerView addSubview:label];
    return headerView;
}

Upvotes: 4

Ahti
Ahti

Reputation: 1430

Simple solution: return a UIView which contains nothing but your desired header view, while giving your headerView the UIViewAutoresizing property flexible right margin, so it won't scale with it's superview (or maybe flexible left margin, or both, depending on what loomk you want ;]).

Upvotes: 0

Related Questions