AAV
AAV

Reputation: 3803

How to add a UIButton on top viewForHeaderInSection

How to add a button on top off viewForHeaderInSection. I like to add a button above section header which can be scrollable with tableview. Any idea how to do this ?

Upvotes: 0

Views: 802

Answers (3)

Sonu
Sonu

Reputation: 248

Use this code, placing it in viewdidload:

- (void)viewDidLoad {
    UIView *headerViews = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];

    UIButton *managePrivacyButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [managePrivacyButton setFrame:CGRectMake(0, 45, 320, 45)];
    managePrivacyButton.titleLabel.textAlignment = UITextAlignmentLeft;
    [managePrivacyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
    [managePrivacyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [headerViews addSubview:managePrivacyButton];
    [self.tableView setTableHeaderView:headerViews];
    [super viewDidLoad];
}

Upvotes: 1

user529758
user529758

Reputation:

The - rather hackish - solution I saw in a pull-to-refresh implementation is to simply add your 'extra' view to the table view as a subview - just make sure it's positionned using a negative y offset. That offset should be equal to (well, rather -1 times) the height of your view. Code:

UIView *myViewAboveHeader = // however you create it
CGRect f = myViewAboveHeader.frame;
f.origin.y = -1 * f.size.height;
myViewAboveHeader.frame = f;
[tableView addSubview:myViewAboveHeader];

Edit: it seems you don't want a header view AND a view above it. In this case, just simply add a button on top of the view you return in your table view delegate method:

- (UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)s
{
    UIView *header = ...;
    UIButton *btn = // create a button somehow;
    [header addSubview:btn];
    return header;
}

Upvotes: 1

J2theC
J2theC

Reputation: 4452

This method in particular takes as a return an UIView. UIButton is a subclass of UIView. Just create a new UIButton and return it.

Upvotes: 0

Related Questions