Ios UIButton图像尺寸与原始尺寸不同
我有一个40x40的按钮,我想在它的中心放置一个20x20尺寸的图像。这就是我所做的Ios UIButton图像尺寸与原始尺寸不同,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我有一个40x40的按钮,我想在它的中心放置一个20x20尺寸的图像。这就是我所做的 将内容模式设置为“中心”。(无缩放) 将图像指定为按钮的“图像”属性 但当我运行程序并检查图像的尺寸时,情况就不同了。我在这里做错了什么吗?在ui按钮类中,设置图像时,图像和背景图像属性具有不同的行为。您可以根据需要使用其中一个按钮。在我的例子中,如果我只设置按钮的“图像”属性,图像将位于中间 检查以下内容: 1.Make sure you set the *image* ,not the *backgroun
但当我运行程序并检查图像的尺寸时,情况就不同了。我在这里做错了什么吗?在
ui按钮
类中,设置图像时,图像
和背景图像
属性具有不同的行为。您可以根据需要使用其中一个按钮。在我的例子中,如果我只设置按钮的“图像”属性,图像将位于中间
检查以下内容:
1.Make sure you set the *image* ,not the *backgroundImage*.
2.Make sure the size of your image assets is smaller than the button's size.
3.Er...I just know two things...
-(void)addImageInCenterForButton:(UIButton *)button
{
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SettingIcon"]];
CGRect imgRect = button.bounds;
imgRect.size.width = 20;
imgRect.size.height = 20;
imgRect.origin.x = (button.bounds.size.width - imgRect.size.width) / 2;
imgRect.origin.y = (button.bounds.size.height - imgRect.size.height) / 2;
imgView.frame = imgRect;
[button addSubview:imgView];
}
试试这个,希望对你有帮助 我的图像是20x20尺寸的,如果我改变按钮的大小,它看起来是一样的,而且它也在按钮的中心
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40 ,40)];
[btn setImage:[UIImage imageNamed:@"ok"] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor redColor]];
[btn setContentMode:UIViewContentModeCenter];
[self.view addSubview:btn];
你能发布不同的图片以便更好地理解答案吗?我想看到图片的原样。没有缩放我已经做了这项工作。我想知道当我们将图像添加为按钮的图像属性时,为什么图像没有正确显示。因为imageView是只读的@属性(非原子、只读、保留)UIImageView*ImageView请理解问题。。图像正在添加,但维度正在更改