0

I put a rounded-rect button into table-view footer.

But the button is streached to the left and to the right no matter how I change the BUTTON_WIDTH. But it's height is adjustable.

What's the problem? Below is the code I used.


#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 45

- (void)viewDidLoad {
    CGRect frame = CGRectMake(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);

// btnSeeResult is decleared in header file
    btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnSeeResult setTitle:@"Result" forState:UIControlStateNormal];
    [btnSeeResult addTarget:self action:@selector(seeResult) forControlEvents:UIControlEventTouchUpInside];
    btnSeeResult.frame = frame;

    self.tableView.tableFooterView = btnSeeResult;
}

2 Answers 2

3

It's because you're setting the button to be the tableView.tableFooterView, so that view's width will always be set to the width of the tableView.

Try using a black UIView as the footer view then adding the button to it, like this:

#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 45

- (void)viewDidLoad {
    CGRect frame = CGRectMake(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);

    // btnSeeResult is decleared in header file
    UIButton *btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnSeeResult setTitle:@"Result" forState:UIControlStateNormal];
    [btnSeeResult addTarget:self action:@selector(seeResult) forControlEvents:UIControlEventTouchUpInside];
    btnSeeResult.frame = frame;

    // the width (320.0) actually doesn't matter here
    UIView *footerView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 80.0, 320.0)] autorelease];
    footerView.backgroundColor = [UIColor clearColor];
    [footerView addSubview:btnSeeResult];

    self.tableView.tableFooterView = footerView;
}
1
  • OK. That works. But few more question. 1. What's the difference between using UIButton as footer and using another UIView? Isn't the button derived from UIView? I think the result should be same. 2. When I was using UIButton directly, it's height could be changed. Just width couldn't be changed. Why is that?
    – SeniorLee
    Dec 29, 2010 at 0:42
2

@SeniorLee as per your question that you left in a comment-

The reason why it wasn't working when you were using the button as the footerview is because the footer view HAS to be the width of the table view. That is why when your button WAS your footerview, it was following the rule that it HAS to be the width of the table.

Now, when you use a regular old UIView, it takes the place of the footerview, and it's width is the same width as the table. Anything you add to that view, then, can be however you want it to be. Does that make sense?

The reason why the height could be changed is because the footerview's height doesn't have the same mandatory stipulations as the width does, so you can change the footer's height all you want, just not the width.

1
  • I thought both UIView and UIButton are NOT streached but actually UIView is streached but it's not drawn, and UIButton can be any size. That makes sense. Thank you very much!
    – SeniorLee
    Dec 30, 2010 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.