Question

I'm using a custom sectionheader class with a tableview, and getting some unexpected behavior after rotation. Here's the use case:

  1. Tap on cell in UITableView
  2. View pushed onto stack.
  3. Rotate the view to landscape.
  4. Rotate back to portrait.
  5. Pop the view.
  6. On the iPhone 3G only, a landscape-sized section header now appears stuck somewhere down the middle of the view (in addition to the portrait-sized section header, which appears, as it should, at the top of the tableview). The extraneous header scrolls with the UITableView cells, and switching away from and back to the view (the UITableView is nested within a UITabBarController) doesn't fix the problem.

I can't reproduce this problem on the iPhone 4, or in the Simulator. It seems that, for some reason, a landscape oriented sectionheaderview is being added to the uitableview after popping the second level view, but why would this be? Note that the same problem is reproduced when the default (and not a custom) header is used. I've also checked whether it's a problem with device orientation being returned incorrectly, and that does not appear to be the case.

Here's the init code for the custom SectionHeaderView class, if it's helpful:

-(id)initWithFrame:(CGRect)frame title:(NSString*)title delegate:(id <SectionHeaderViewDelegate>)aDelegate {

self = [super initWithFrame:frame];

if (self != nil) {

    float lineHeight = 0.5f;
    // check line sizing for retina/non-retina
    if (![Utilities hasRetina])
    {
        lineHeight = 1.0f;
    }

    // Set up the tap gesture recognizer.
    delegate = aDelegate;        

    // Create and configure the title label.
    CGRect titleLabelFrame = self.bounds;
    titleLabelFrame.origin.y -= 12.5;
    titleLabel = [[UILabel alloc] initWithFrame:titleLabelFrame];
    titleLabel.text = title;
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.font = [UIFont fontWithName:@"Georgia" size:15.0];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:titleLabel];

    // add thin white line to top of section header
    UIView *topBorder = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.bounds.size.width, lineHeight)];
    [topBorder setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:topBorder];
    [topBorder release];

    // Set the colors for the gradient layer.
    static NSMutableArray *colors = nil;
    if (colors == nil) {
        colors = [[NSMutableArray alloc] initWithCapacity:3];
        UIColor *color = nil;
        color = [UIColor colorWithRed:57.0/255.0 green:56.0/255.0 blue:105.0/255.0 alpha:1.0];
        [colors addObject:(id)[color CGColor]];
        color = [UIColor colorWithRed:54.0/255.0 green:53.0/255.0 blue:95.0/255.0 alpha:1.0];
        [colors addObject:(id)[color CGColor]];
        color = [UIColor colorWithRed:57.0/255.0 green:56.0/255.0 blue:105.0/255.0 alpha:1.0];
        [colors addObject:(id)[color CGColor]];
    }
    [(CAGradientLayer *)self.layer setColors:colors];
    [(CAGradientLayer *)self.layer setLocations:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.48], [NSNumber numberWithFloat:1.0], nil]];
}

return self;
}

Why would an additional landscape version of the custom SectionHeaderView be added in the portrait view, only on the iPhone 3G?

Thanks in advance for any help.

Was it helpful?

Solution

Still no clue what's causing this problem, but I eventually solved it by adding a check in viewWillAppear to make sure the old header was removed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top