简体   繁体   English

UIImage显示为不透明正方形(IOS Swift)

[英]UIImage being displayed as opaque square (IOS Swift)

So I have this Image view: 所以我有这个图像视图:

在此处输入图片说明

When I add it as a button's UIImage, the resulting button is simply a pink opaque square that is filled in. I don't understand why this is happening. 当我将其添加为按钮的UIImage时,得到的按钮只是一个填充的粉红色不透明方形。我不明白为什么会这样。

This is happening because the image is not the correct size. 发生这种情况是因为图像尺寸不正确。 In your code, get on the largest device simulator and print the UIButton.frame inside viewDidAppear(). 在您的代码中,使用最大的设备模拟器,并在viewDidAppear()中打印UIButton.frame。 Once you know what the frame size is, make the image the same size instead of smaller and stretching to fit. 一旦知道了帧的大小,就使图像具有相同的大小,而不是变小并拉伸以适合。 As the image is stretched, it will make itself look more opaque as it were. 随着图像的拉伸,它将使自身看起来更加不透明。

In other advice, I would recommend not using an image for somethign so simple. 在其他建议中,我建议不要将图像用于如此简单的东西。 Use this: 用这个:

    yourButton.layer.borderWidth = 2
    yourButton.layer.cornerRadius = 5
    yourButton.layer.borderColor = UIColor(red: 249/255, green: 0/255, blue: 150/255, alpha: 1/1).cgColor

I used your actual color for the borderColor, so you are good to go. 我使用了您的实际颜色作为borderColor,所以一切顺利。 Play with the borderWidth and cornerRadius until they are what you want. 使用borderWidth和cornerRadius进行播放,直到它们成为您想要的为止。

If you do decide to stick with an image, you are better off with a larger image than a smaller one for something like this, and make sure that you assign it to the background image of the button, because you want to to function as a border and it will function better that way. 如果您决定坚持使用图像,则最好使用较大的图像而不是较小的图像,并且要确保将其分配给按钮的背景图像,因为您想用作图像。边框,这样效果会更好。

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

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