mjanisz1
mjanisz1

Reputation: 1516

Unchangable button width and height

I want to resize a button programatically, but modifying the parameters gives no effect.

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

Any idea why? i can give whatever amount i like to the parameters, but result is the same - width is bounded to text length.

Upvotes: 0

Views: 106

Answers (4)

Sam B
Sam B

Reputation: 27618

I took your code and tested it in iOS 6.0. This is the final version of what you need to do

Add QuartzCore Library and add this to your header file

#import <QuartzCore/QuartzCore.h> 

Now here's your button code with a pic. Play around with width and hight and they will change

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

Upvotes: 1

subhash kumar singh
subhash kumar singh

Reputation: 2756

use [UIButton buttonWithType:UIButtonTypeCustom] with appropriate value, button will resize.

UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd

Upvotes: 0

Ben
Ben

Reputation: 607

you almost certainly have autolayout turned on. Go into interface builder and turn it off and your code should work

Upvotes: 0

Yaman
Yaman

Reputation: 3991

Try to change btnCancel = [UIButton buttonWithType:102]; by btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];

Upvotes: 0

Related Questions