我有一个40x40按钮,在其中我想在中心放置一个20x20尺寸的图像。这就是我所做的。
但是当我运行程序并检查图像的尺寸时,它就不同了。我在这里做错了吗?
答案 0 :(得分:0)
在UIButton
课程中,您设置图片时具有不同行为的image
和backgroundImage
属性。您可以根据自己的意愿使用其中一个。
答案 1 :(得分:0)
在我的情况下如果我只是设置按钮的图像属性,图像将位于中心。
检查以下内容:
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...
答案 2 :(得分:0)
请参阅下面的代码段,它会将图像添加到按钮的中心。
-(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];
}
答案 3 :(得分:0)
尝试这个我希望它有所帮助..
我的图片尺寸为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];