当前位置: 代码迷 >> Iphone >> 如何为UIButton添加背景图片
  详细解决方案

如何为UIButton添加背景图片

热度:65   发布时间:2016-04-25 06:06:34.0
怎么为UIButton添加背景图片?
怎么为UIButton添加背景图片

就是让button变成一个看样子像图片的按钮把 我的理解是这样的 但是不知道怎么具体实现

------解决方案--------------------
setImage or setBackgroundImage,方法直接在UIButton.h文件里可以找到
------解决方案--------------------
工程里放置一个名为 "正常状态.png"的图片,然后设置代码如下。
[button setImage:[UIImage imageNamed:@"正常状态"] forState:UIControlStateNormal];

------解决方案--------------------
楼上皆正解。如果用IB,选中按钮,直接在Attributes inspector里有image和Backgroud属性
------解决方案--------------------
需要设置按钮的属性  代码如下
 UIButton *myBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [myBtn setFrame:CGRectMake(0, 0, 100, 100)];//注意设置为图片大小,不然会出现模糊
    [myBtn setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
    [self.view addSubview:myBtn];