开发者

Adjusting button size in iPhone

开发者 https://www.devze.com 2023-02-01 06:55 出处:网络
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.

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


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


@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.

0

精彩评论

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