Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton的大小与封闭的UIImageView对象不同_Ios_Objective C_Uiimageview_Uibutton - Fatal编程技术网

Ios UIButton的大小与封闭的UIImageView对象不同

Ios UIButton的大小与封闭的UIImageView对象不同,ios,objective-c,uiimageview,uibutton,Ios,Objective C,Uiimageview,Uibutton,我在UIButton对象中放置了一些视图,并使用setImage:forState:method将图像设置为按钮。 图像大小为19x8。 并且,调用[button SetTranslatesAutoResizengMaskintoConstraints:NO]方法来调整自动布局。自动布局只是设置按钮的位置。 然后,我发现按钮大小是19x22,附带的图像大小是19x8。图像位置垂直居中 为什么按钮与图像不同? 我怎样才能把它们做成同样的尺寸 我添加了示例代码 UIButton *button =

我在UIButton对象中放置了一些视图,并使用setImage:forState:method将图像设置为按钮。 图像大小为19x8。 并且,调用[button SetTranslatesAutoResizengMaskintoConstraints: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;

如果您列出按钮的约束条件,以及它们的实际代码,这将非常有用。嗯。。。不幸的是,我不使用设置帧,因为使用自动布局。使用自动布局,按钮的矩形会根据设备而改变?请检查编辑,此代码会将按钮大小更改为图像大小,而不会更改其位置