按钮宽度和高度不变

时间:2013-06-25 20:12:43

标签: ios uibutton

我想以编程方式调整按钮的大小,但修改参数不起作用。

btnCancel = [UIButton buttonWithType:102];
[btnCancel setFrame:CGRectMake(22.0f, 7.0f, 40.0f, 40.0f)];
[btnCancel setTitle:@"Anuluj" forState:UIControlStateNormal];
[btnCancel setTintColor:[UIColor redColor]];
[btnCancel addTarget:self action:@selector(cancelTyping) forControlEvents:UIControlEventTouchUpInside];

知道为什么吗?我可以给参数提供任何数量,但结果是相同的 - 宽度限制为文本长度。

4 个答案:

答案 0 :(得分:1)

我拿了你的代码并在iOS 6.0中进行了测试。这是您需要做的最终版本

添加QuartzCore库并将其添加到头文件

#import <QuartzCore/QuartzCore.h> 

现在这是带有图片的按钮代码。玩弄宽度和高度,他们会改变

UIButton *btnCancel =[UIButton buttonWithType:UIButtonTypeCustom];
    [btnCancel setFrame:CGRectMake(22.0f, 7.0f, 80.0f, 80.0f)];
    [btnCancel setTitle:@"Anuluj" forState:UIControlStateNormal];
    btnCancel.backgroundColor = [UIColor redColor];
    btnCancel.layer.borderColor = [UIColor redColor].CGColor;
    btnCancel.layer.borderWidth = 0.5f;
    btnCancel.layer.cornerRadius = 10.0f;
    [btnCancel addTarget:self action:@selector(cancelTyping) forControlEvents:UIControlEventTouchUpInside];

enter image description here

答案 1 :(得分:0)

尝试按btnCancel = [UIButton buttonWithType:102];

更改btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];

答案 2 :(得分:0)

你几乎肯定已经开启了自动布局。进入界面构建器并将其关闭,您的代码应该可以正常工作

答案 3 :(得分:0)

使用具有适当值的[UIButton buttonWithType:UIButtonTypeCustom],按钮将调整大小。

UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd
相关问题