简体   繁体   中英

How to disable UIButton Tint Color?

I know my case may be rare but how do you disable the UIButton tint colour in this case?

I have a customised UIButton which has attributedTitle to help display the button pattern in different colour and alpha.

在此处输入图片说明

In my customised button .m file I have set something like this

[self setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateSelected];

which will make the background colour to gray when the button is selected

However the real result looks like this:

在此处输入图片说明

Some how the text colour gets turned into white which I think is because of the tint effect on UIButtons.

Is that possible for me to have the background as grey while the text still remain the colour as set in the attributed title on selected state of the button?

Thanks

yourbutton = [UIButton buttonWithType:UIButtonTypeCustom];

                        (or)

If u placed button in storyboard....Choose button type as custom instead of system.

You can override setImage:forState: in UIButton subclass and change rendering mode to .alwaysOriginal .

override func setImage(_ image: UIImage?, for state: UIControlState) {
    let newImage = image?.withRenderingMode(.alwaysOriginal)
    super.setImage(newImage, for: state)
}

Simple, just head to the image in your assets, and set its rendering mode to "Original Image" , check image below:

在此处输入图片说明

Follow this steps to get result:

  • First set UITabBar background color to clear
  • Now put this line in viewDidLoad method of the first page:

    [[UITabBar appearance] setTintColor:SETYOURCOLOR];

Hope it will help you.

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