简体   繁体   中英

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.

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(). 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. Play with the borderWidth and cornerRadius until they are what you want.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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