简体   繁体   English

为什么分配图像后我的UIButton更改大小和位置? 如何保持相同?

[英]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. 我给它分配图像后,UIButton的大小和位置会发生变化。 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. 我应该设置backgroundImage而不是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"]]; 答案:[按钮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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改图片后如何将UIButton保持在同一位置? - How can I keep a UIButton in same location after changing the Image? 重新加载数据后,我可以在我的 tableView 中保持相同的位置吗? - Can I keep the same position in my tableView after reloading the data? 设置UIButton的框架,但保持图像大小相同 - Set frame of UIButton but keep image same size 如何设置UIButton图像的大小和位置 - how to set size and position of UIButton's image 更改 UIButton 的图像后如何更新约束 - How do I update constraints after changing UIButton's image 如何在表格视图中保持图像大小? - how can i keep image size in tableview? 如何设置UI元素的大小与在iPhone 6/6 +上添加启动图像之前的大小相同? - How can I set keep UI elements the same size they were prior to adding a launch image on the iPhone 6/6+? 为什么我的UIButton图像大小在模拟器和设备中有所不同? - Why is my UIButton Image size different in Simulator and Device? 动画后如何将UIImage保持在新位置? - How can I keep UIImage in new position after Animation? 迅捷将图像移回中间后,如何带上图像? 或将其旋转回原点位置 - Swift How can I bring my Image after moving it back to the Middle ? Or rotate it back to the Origin position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM