Search code examples
uitableviewaddsubview

Can't get UILabel to show properly on top of UITableView


I am struggling to achieve what I thought was nothing but a 1' coding but apparently adding a UILabel above my UITableView in a UITableViewController is not a piece of cake...?

Here is the code (yes basic, I know):

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 310, 20)];
    [[self tableView] addSubview:label];

The result can be seen in the screenshot below, the label on the top right is just half displayed, saying "Balance..."

Please note that if I try to change CGRect origin.y or size.height the UILabel is not displayed at all.

I also tried adding the following, with no change in result:

    [[self tableView] bringSubviewToFront:balanceLabel];

I don't care if the UILabel is scrolled up when scrolling up the UITableView, I want it to stick with the first section header.

I know this can be achieved in other ways, using a custom UIView for the header, changing to UIViewController or using a .xib, but really I would like to understand why this happens.

Thanks for any help. F.

How the label appears


Solution

  • It does look like the header is hiding your label, maybe you could try setting the header background to clearColor. Since you have no control on the table view loop I suspect that after your addSubView somewhere the table builds its own header and does another addSubView.