我之前做过一个项目,我将 UIButton 设置为既有文本又有图像(在界面构建器中).
I have made a project previously where I set a UIButton to have both text and also an image (in interface builder).
我创建了另一个项目,我想实现同样的目标,但是当我将按钮设置为有图像时,标题消失了(它没有隐藏在图像后面).
I have created another project and I want to achieve the same thing, but when I set the button to have an image, the title disappears (it's not getting hidden behind the image).
我似乎无法弄清楚为什么会这样,如果有人能对这个主题有所了解,我将不胜感激!
I can't seem to figure out why this is so if anyone could shed some light on this subject I'd appreciate it!
谢谢.
将您的按钮更改为自定义类型.放上图片,然后放上文字.
Change your button to custom type. put the image on then put the text in.
您需要在其上放置自己的背景,但此时您将能够使用边缘设置来定位文本和图像.
You will need to put your own background on it, but at that point you will be able to use the edge settings to position both the text and the image.
我拥有的每个带有图像和文本的按钮都设置为自定义,最终也有自己的背景图像.
Every button that I have with image and text is set to custom and ultimately has its own bg image as well.
哦,我用的另一个东西.是角半径.为视图提供与默认按钮一样的圆角效果.
Oh, another thing I use. Is the cornerRadius. To give the view the rounded effect like the buttons have by default.
saveButton.layer.cornerRadius = roundingNumber.floatValue;
此外,您应该能够将borderWidth 和borderColor 与cornerRadius 结合使用来获得一个看起来与原始按钮非常相似的按钮
Additionally you should be able to use the borderWidth and borderColor in conjunction with cornerRadius to get a button that looks quite similar to the original button
saveButton.layer.borderRadius = 1.0f;
saveButton.layer.borderColor = [[UIColor blueColor] CGColor];
// These are not the exact values (or maybe they are) but you get the idea.
希望有帮助.
更正
在花了一点时间调查之后,当您将图像放入按钮时,它似乎是.文本被推到右边.
After Spending a little time investigating, it appears that when you put an image in the button. the text is shoved off to the right.
使用边缘设置将其重新覆盖图像.
Use the edge settings to bring it back over the image.
选中按钮后,查看属性检查器">按钮>边缘>(下拉标题")
With the button selected, Look in "Attributes Inspector" > Button > Edge > (Dropdown "Title")
我想出的等式是
[Edge inset for Title] Left = -(imageWidth * 2)
这应该将您的标题与您放入的图像左对齐.
This should Left align your title with the image that you have put in.
需要进行自定义定位以使其看起来正确.
Custom positioning will need to be done to make it look correct.
这可以完全在 Interface Builder 中完成,您可以使用 RoundedRect 按钮样式
这里有一些展示变化的图片.
Here are some images demonstrating the change.
这里我将 Button 类型设置为自定义"以移除边框和背景.
Here I set the Button type to "Custom" to remove the border and background.
这里我为标题"设置了顶部和左侧(标题是下拉菜单中的一个选项)
Here I set the Top and Left in the Edge set for "Title" (Title is an option in the drop down)
编辑
最新的 Xcode 已将尺寸设置移至新位置
Newest Xcode has moved the size settings to a new location
这篇关于iOS - 界面生成器:设置图像时 UIButton 标题消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!