开发者

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 tric开发者_如何学JAVAk to it, or a better way?


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;
}


Please use 2 frames for button one for landscape mode and another for portrait mode..this will work out..so when orientatin changes check whetehr its landscape or portrait and assign frame for it...check mode here willrotatetointerfaceorientation


Best i've come up with is, use one button and one cell in its own section, then each will resize appropriately.


I do something very similar to yours except I only have one button. the uiview that you are returning should be the same width as the tableView itself.

CGFloat footerWidth = mainTableView.frame.size.width;
CGRect frameTableFooter = CGRectMake(0.0, 0.0, footerWidth, 60.0);  
viewForTableFooter.frame = frameTableFooter;
// buttons addition
mainTableView.tableFooterView = viewForTableFooter;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜