我需要一个带有图像和文本的UIButton
。图像应该在顶部&文本在图像下面,两者都应该是可点击的。
发布于 2012-08-07 21:45:52
我看到了非常复杂的答案,它们都使用代码。但是,如果您使用的是接口生成器,则有一种非常简单的方法可以做到这一点:
您甚至可以通过代码使用相同的方法,而无需像其他解决方案那样在内部创建UILabels和UIImages。永远保持简单!
编辑:附加了一个小示例,其中设置了3项内容(标题、图像和背景),并添加了正确的插图
发布于 2012-08-03 17:17:31
我认为你正在寻找这个解决方案来解决你的问题:
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setFrame:CGRectMake(0.f, 0.f, 128.f, 128.f)]; // SET the values for your wishes
[_button setCenter:CGPointMake(128.f, 128.f)]; // SET the values for your wishes
[_button setClipsToBounds:false];
[_button setBackgroundImage:[UIImage imageNamed:@"jquery-mobile-icon.png"] forState:UIControlStateNormal]; // SET the image name for your wishes
[_button setTitle:@"Button" forState:UIControlStateNormal];
[_button.titleLabel setFont:[UIFont systemFontOfSize:24.f]];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // SET the colour for your wishes
[_button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; // SET the colour for your wishes
[_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, -110.f, 0.f)]; // SET the values for your wishes
[_button addTarget:self action:@selector(buttonTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside]; // you can ADD the action to the button as well like
...the按钮的其余定制工作现在由您负责,不要忘记将按钮添加到您的视图中。
更新#1和更新#2
或者,如果你不需要动态按钮,你可以把你的按钮添加到界面生成器的视图中,你也可以在那里设置相同的值。这是非常相同的,但这是这个版本以及在一个简单的图片。
您还可以在界面构建器中看到屏幕截图上的最终结果。
发布于 2017-09-07 23:50:48
Xcode-9和Xcode-10苹果对边缘嵌入做了很少的更改,你可以在尺寸检查器下对其进行更改。
请按照以下步骤操作:
第-1步:输入文本并选择要显示的图像:
Step-2:根据需要选择按钮控件,如下图所示:
第-3步:现在转到尺寸检查器,并根据您的要求添加价值:
https://stackoverflow.com/questions/11717219
复制相似问题