简体   繁体   English

iOS - 界面生成器:设置图像时 UIButton 标题消失

[英]iOS - Interface Builder: UIButton title disappears when setting image

I have made a project previously where I set a UIButton to have both text and also an image (in interface builder).我之前做过一个项目,我将 UIButton 设置为既有文本又有图像(在界面构建器中)。

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!我似乎无法弄清楚为什么会这样,如果有人能对这个主题有所了解,我将不胜感激!

Thanks.谢谢。

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.我拥有的每个带有图像和文本的按钮都设置为自定义,最终也有自己的 bg 图像。

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;

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此外,您应该能够将 borderWidth 和 borderColor 与 cornerRadius 结合使用来获得一个看起来与原始按钮非常相似的按钮

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.

hope that helps.希望有帮助。

Correction更正

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")选择按钮后,查看“属性检查器”>“按钮”>“边缘”>(下拉“标题”)

the equasion I have come up with is我提出的方程是

[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.需要进行自定义定位以使其看起来正确。

This can be done entirely in the Interface Builder and you can use RoundedRect button style这可以完全在 Interface Builder 中完成,您可以使用 RoundedRect 按钮样式

Here are some images demonstrating the change.以下是一些演示更改的图像。

Here I set the Button type to "Custom" to remove the border and background.在这里,我将 Button 类型设置为“自定义”以移除边框和背景。

自定义已设置

Here I set the Top and Left in the Edge set for "Title" (Title is an option in the drop down)在这里,我为“标题”设置了边缘设置中的顶部和左侧(标题是下拉列表中的一个选项)

Left 和 Top 已设置

EDIT编辑

Newest Xcode has moved the size settings to a new location最新的 Xcode 已将大小设置移至新位置

在此处输入图片说明

Just put the image in the background field instead.只需将图像放在背景字段中即可。 This way the text will appear in front of the image.这样文本就会出现在图像的前面。

听起来您正在为按钮设置图像而不是背景图像,因此将其反转。

I noticed that if the UIButton colour is set to default, adding an image makes it white and thus invisible.我注意到,如果 UIButton 颜色设置为默认值,添加图像会使它变白,因此不可见。 Changing the colour manually makes it visible again.手动更改颜色使其再次可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM