简体   繁体   English

UIButton将内容保持在backgroundImage顶部

[英]UIButton keep content on top of backgroundImage

I have a UIButton and I'm setting a background image on it: 我有一个UIButton,并在上面设置了背景图片:

[self.button setBackgroundImage:[UIImage imageNamed:@"button-background"] forState:UIControlStateNormal];

I want the content of the button to stay on top of the background image, and not be affected by it. 我希望按钮的内容保持在背景图片的上方,并且不受其影响。 So when the button content is text, the font color of the text would be unaffected by changing the background image. 因此,当按钮内容为文本时,更改背景图像不会影响文本的字体颜色。 I'd like it to behave similarly when the button content is an image. 当按钮内容是图像时,我希望它的行为类似。

What's the best way to achieve this behavior? 实现此行为的最佳方法是什么?

Setting the background of the button does exactly what you're asking. 设置按钮的背景完全符合您的要求。 However, make sure your button is in the state you're setting the background image for. 但是,请确保您的按钮处于您要为其设置背景图像的状态。 Text will appear over this image based on your settings. 根据您的设置,文本将显示在该图像上。

forState:UIControlStateNormal (Highlighted, selected, etc)

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

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