Search code examples
iosuibuttonsizetofit

how to fix width in UIButton's sizeToFit/sizeThatFit?


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?


Solution

  • If you want to change the size set by sizeToFit, you need to override the sizeThatFits method. Create a subclass of UIButton as follows:

    Swift:

    class MyButton: UIButton {
        override func sizeThatFits(_ size: CGSize) -> CGSize {
            let sizeThatFits = super.sizeThatFits(size)
            return CGSize(width: sizeThatFits.width, height: self.frame.height)
        }
    }
    

    Objective C:

    @implementation MyButton
    
    - (CGSize)sizeThatFits:(CGSize)size {
        CGSize sizeThatFits = [super sizeThatFits:size];
        return CGSizeMake(sizeThatFits.width, self.frame.size.height);
    }
    
    @end
    

    And use the subclassed button instead of UIButton.

    UIButton* btn = [MyButton buttonWithType:UIButtonTypeCustom];
    

    Reference: Apple Doc: sizeToFit