简体   繁体   中英

why is my UIButton changing size and position after assigning an image? How can I keep it the same?

my UIButton size and position changes after I assign an image to it. I am not sure why. Does anyone know?

I tried repositioning the button to its original position but it doesnt work.

CGRect buttonFrame = _subscribeButton.frame;
CGAffineTransform transform = _subscribeButton.transform;

[_subscribeButton setTitle:@"Subscribe" forState:UIControlStateNormal];

[_subscribeButton setImage:[UIImage imageNamed:@"bigButton.png"] forState:UIControlStateNormal];

[_subscribeButton setFrame:buttonFrame];
[_subscribeButton setTransform:transform];

I should be setting backgroundImage not image. this was the problem the whole time. aspect fill only effects the background image as this is scaled by the frame.

answer: [button setBackgroundImage:[UIImage imageNamed:@"blah.png"]];

change this

CGAffineTransform transform = _subscribeButton.transform;

to

CGAffineTransform transform = CGAffineTransformIdentity;

Update:

CGRect buttonFrame = _subscribeButton.frame;

CGAffineTransform transform = CGAffineTransformIdentity;

[_subscribeButton setTitle:@"Subscribe" forState:UIControlStateNormal];

[_subscribeButton setImage:[UIImage imageNamed:@"bigButton.png"] forState:UIControlStateNormal];

[_subscribeButton setContentMode:UIViewContentModeScaleToFill];

_subscribeButton.autoresizingMask = UIViewAutoresizingNone;

_subscribeButton.autoresizesSubviews = NO;



[_subscribeButton setFrame:buttonFrame];

[_subscribeButton setTransform:transform];

try this

 CGRect buttonFrame = _subscribeButton.frame;
    CGAffineTransform transform = CGAffineTransformIdentity;

    [_subscribeButton setTitle:@"Subscribe" forState:UIControlStateNormal];

    [_subscribeButton setImage:[UIImage imageNamed:@"bigButton.png"] forState:UIControlStateNormal];

    [_subscribeButton setFrame:buttonFrame];
    [_subscribeButton setTransform:transform];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM