1

I have a UITableView with 5 different sections. For one of those sections I have added a UIButton by doing:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    if (section == 5) {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    [button1 setTitle:@"Button" forState:UIControlStateNormal];


    button1.frame = CGRectMake(8, 8, 96, 44);



    [view addSubview:button1];


    return view;
    }
    return nil;
}

The problem that I am having is this: Since the Header for a section is by default aligned to the left, button is being aligned to the left, I have looked all around for a solution but can't seem to find one, your help/advice is appreciated!

4

2 回答 2

2

真的,这里没有任何东西可以自行对齐。您在这里所做的是为您的部分标题创建一个画布视图,这是正确的,然后您将按钮放置在(8, 8)其原点大致在左上角。只需设置适当的原点坐标,您就会被设置。

您应该在此处查看 CGRect* 系列函数,以使其更容易。

于 2013-07-17T04:39:02.190 回答
2

没有对齐因素,只是设置按钮框架的点将其 button1.frame = CGRectMake(8, 8, 96, 44);设置为以视图为中心,然后它将显示为

于 2013-07-17T04:43:59.560 回答