我想以编程方式调整按钮的大小,但修改参数无效。

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


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

最佳答案

我接受了您的代码,并在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];

10-08 05:48