I found the sizeToFit or sizeThatFits will change UIButton 's current width.
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 200, 40);
btn.backgroundColor = [UIColor redColor];
[btn.titleLabel setText:@"ssdkfjsakdfjsakfjaksdjfkasjfkasjkfasjkfsajfkasjkas"];
btn.titleLabel.numberOfLines = 0;
[btn setTitle:@"ssdkfjsakdfjsakfjaksdjfkasjfkasjkfasjkfsajfkasjkas" forState:UIControlStateNormal];
[self.view addSubview:btn];
NSLog(@"before sizeToFit: %@",NSStringFromCGRect(btn.frame));
CGSize size = [btn sizeThatFits:CGSizeMake(200, 1000)];
[btn sizeToFit];
NSLog(@"after sizeToFit:%@",NSStringFromCGRect(btn.frame));
NSLog(@" sizeThatFits:%@",NSStringFromCGSize(size));
and the output will be:
before sizeToFit: {{100, 100}, {200, 40}}
after sizeToFit:{{100, 100}, {392, 34}}
sizeThatFits:{392, 34}
this is not the behavior I want , if I want the sizeToFit resizing the button frame to its proper height based on its origin width. how to achive this?
If you want to change the size set by sizeToFit, you need to override the sizeThatFits method. Create a subclass of
UIButtonas follows:Swift:
Objective C:
And use the subclassed button instead of UIButton.
Reference: Apple Doc: sizeToFit