Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a UIButton in a UITableView footer when orientation changes

I'm using two UIButtons in a footer of a UITableView (both subviews of a single UIView). I am not using a UITableViewCell because I wanted to skin the button so it looks like the red Delete button at the bottom of some iPhone screens such as when editing a contact.

It is sized and works correctly. However, the table will resize itself on device orientation changes (landscape, portrait and so on) and the button stays its original width. I tried using autoresizing masks but nothing worked.

Is there a trick to it, or a better way?

like image 595
akaru Avatar asked May 18 '11 10:05

akaru


1 Answers

It should work with autoresizingmasks, I've done it before but it's important to set the width of your view correctly and add the correct sizingmasks.

Some sample code to show how it works. This creates two buttons resizing whem you rotate.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];

    view.backgroundColor = [UIColor redColor];

    UIButton *buttonA = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonA.frame = CGRectMake(20, 5, 125, 40);
    [buttonA setTitle:@"ButtonA" forState:UIControlStateNormal];
    buttonA.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    [view addSubview:buttonA];

    UIButton *buttonB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonB.frame = CGRectMake(175, 5, 125, 40);
    [buttonB setTitle:@"ButtonB" forState:UIControlStateNormal];
    buttonB.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    [view addSubview:buttonB];

    return [view autorelease];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
like image 109
Mac_Cain13 Avatar answered Oct 20 '22 07:10

Mac_Cain13