Image vs background image for UIButton

I have a 480x331 image and have a transparent background. The transparent part is only around the edges of the image. My UIButton is 72x37. When I set the image as the background image in IB for the button, the transparent edges of the image appear white in the button. Without image, the button is white with rounded corners. I guess this is the white that I see. If so, then the transparency of the image works, but the white area of ​​the button shows.

If I set the image to the UIButton Image property, it does not scale correctly even after changing the view mode and remains in full size.

Any suggestions on which property I should use for proper scaling and transparency?

+3
source share
2 answers

What type of button are you using?

By sounds, you probably use the UIButtonTypeRoundedRect style, but UIButtonTypeCustom might be more appropriate. You can change this in the interface inspector window.

+7
source

@ Christopher Fairheim has the right to propose the use of UIButtonTypeCustom. Using UIButtonTypeRoundedRect will give you a white background in transparent areas.

However, connecting objects in Interface Builder has its limitations. If you want to dynamically generate UIViews, UIButtons and assets from a dataset or based on user input, doing this programmatically is the way to go.

: (: UIButtonTypes, UIButtonTypeCustom .)

UIButton *btnName = [UIButton buttonWithType:UIButtonTypeCustom];

:

btnName.backgroundColor = [UIColor clearColor];
btnName.frame = CGRectMake(100, 100, 72, 37);  //(X coord, Y coord, width, height)
[btnName addTarget:myActionClassName action:@selector(myActionName) forControlEvents:UIControlEventTouchUpInside];

(, .png):

UIImage *btnNameImageNormal = [UIImage imageNamed:@"btnNameNormal.png"];
[btnName setBackgroundImage:btnMenuImageNormal forState:UIControlStateNormal];
UIImage *btnNameImagePressed = [UIImage imageNamed:@"btnNamePressed.png"];
[btnName setBackgroundImage:btnNameImagePressed forState:UIControlStateHighlighted];

( viewController):

[self addSubview:btnName];

( , .)

+6

Source: https://habr.com/ru/post/1721016/


All Articles