调整iPhone中的按钮大小

调整iPhone中的按钮大小,iphone,uibutton,Iphone,Uibutton,我在表视图的页脚中放了一个圆形的rect按钮 但无论我如何改变按钮的宽度,按钮都会向左和向右伸展。但它的高度是可调的 有什么问题吗?下面是我使用的代码 #define BUTTON_WIDTH 80 #define BUTTON_HEIGHT 45 - (void)viewDidLoad { CGRect frame = CGRectMake(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT); // btnSeeResult is decleared in he

我在表视图的页脚中放了一个圆形的rect按钮

但无论我如何改变按钮的宽度,按钮都会向左和向右伸展。但它的高度是可调的

有什么问题吗?下面是我使用的代码


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

这是因为您将按钮设置为
tableView.tableFooterView
,因此视图的宽度将始终设置为
tableView
的宽度

尝试使用黑色
ui视图
作为页脚视图,然后向其添加按钮,如下所示:

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

这是因为您将按钮设置为
tableView.tableFooterView
,因此视图的宽度将始终设置为
tableView
的宽度

尝试使用黑色
ui视图
作为页脚视图,然后向其添加按钮,如下所示:

#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-

当您使用按钮作为页脚视图时,它不起作用的原因是页脚视图必须是表视图的宽度。这就是为什么当按钮是footerview时,它遵循的规则是它必须是表的宽度

现在,当您使用常规的旧UIView时,它将取代footerview,并且它的宽度与表的宽度相同。那么,您添加到该视图中的任何内容都可以是您希望的样子。这有意义吗


之所以可以更改高度,是因为页脚视图的高度没有与宽度相同的强制性规定,因此您可以随意更改页脚高度,而不是宽度。

@SeniorLee根据您在评论中留下的问题-

当您使用按钮作为页脚视图时,它不起作用的原因是页脚视图必须是表视图的宽度。这就是为什么当按钮是footerview时,它遵循的规则是它必须是表的宽度

现在,当您使用常规的旧UIView时,它将取代footerview,并且它的宽度与表的宽度相同。那么,您添加到该视图中的任何内容都可以是您希望的样子。这有意义吗


之所以可以更改高度,是因为页脚视图的高度没有与宽度相同的强制性规定,因此您可以随意更改页脚高度,而不是宽度。

OK。这很有效。但没有更多的问题。1.使用UIButton作为页脚和使用其他UIView有什么区别?按钮不是从UIView派生的吗?我认为结果应该是一样的。2.当我直接使用UIButton时,它的高度可以改变。只是宽度无法更改。为什么?好的。这很有效。但没有更多的问题。1.使用UIButton作为页脚和使用其他UIView有什么区别?按钮不是从UIView派生的吗?我认为结果应该是一样的。2.当我直接使用UIButton时,它的高度可以改变。只是宽度无法更改。为什么?我以为UIView和UIButton都没有拉伸,但实际上UIView是拉伸的,但没有绘制,UIButton可以是任何大小。这是有道理的。非常感谢你!我以为UIView和UIButton都不是拉伸的,但实际上UIView是拉伸的,但没有绘制,UIButton可以是任何大小。这是有道理的。非常感谢你!