繁体   English   中英

UIButton的大小不同于随附的UIImageView对象

[英]UIButton's size is different to the enclosed UIImageView object

我放一些UIButton对象的视图,并使用setImage:forState:方法将图像设置为按钮。 图像尺寸为19x8。 并且,调用[button setTranslatesAutoresizingMaskIntoConstraints:NO]方法来调整自动版式。 自动布局仅是按钮的设置位置。 然后,我发现按钮的大小为19x22,随附的图像大小为19x8。 图像位置垂直居中。

为什么按钮与图像不同? 如何将它们制作成相同大小?


我添加了示例代码。

UIButton *button = [[UIButton alloc] initWithFame:CGRectZero];
[button setImage:[UIImage imageNamed:@"buttonImage"] forState:UIControlStateNormal]];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];

而已。

如果要使按钮与图像大小相同,则首先获取图像的大小。

CGFloat width = myImage.size.width;
CGFloat height = myImage.size.height;

CGRect myRect = CGSizeMake:(myButton.frame.size.x,myButton.frame.size.y,width,height); //x and y with respect with superview.
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.frame = myRect;

暂无
暂无

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

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