为什么我的UIButton在分配图像后会改变大小和位置?我怎么能保持一样?

时间:2014-02-24 11:40:20

标签: ios uibutton uiimage storyboard

我为其分配图像后,我的UIButton尺寸和位置发生了变化。我不知道为什么。有人知道吗?

我尝试将按钮重新定位到原来的位置,但它不起作用。

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

3 个答案:

答案 0 :(得分:1)

我应该设置backgroundImage而不是图像。这是整个问题。纵横填充仅影响背景图像,因为这是由框架缩放的。

回答:[button setBackgroundImage:[UIImage imageNamed:@“blah.png”]];

答案 1 :(得分:0)

change this

CGAffineTransform transform = _subscribeButton.transform;

to

CGAffineTransform transform = CGAffineTransformIdentity;

<强>更新

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

答案 2 :(得分:0)

试试这个

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