UIButton background color overlaps text on highlight

半世苍凉 提交于 2020-12-10 08:02:34

问题


After I set my UIButton's backgroundColor and textColor for state highlighted the following output is presented:

The problem here is that the button's background overlaps the white text. How can I solve this?

I also have lots of problem on tvOS with setting the background and text color from the Interface Builder (not working from State Config). I have to make a combination of the IB's properties and code.

Here is my code:

if shopNowButton.highlighted == true {
    shopNowButton.highlighted = false
    shopNowButton.backgroundColor = UIColor.orangeColor()
    shopNowButton.setTitleColor(UIColor.whiteColor(), forState: .Highlighted)

}
else {
    shopNowButton.highlighted = true
    shopNowButton.backgroundColor = UIColor.whiteColor()
    shopNowButton.layer.borderWidth = 1
    shopNowButton.layer.borderColor = UIColor.orangeColor().CGColor
    shopNowButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
}

回答1:


I believe the UIControlState you're actually looking for is Focused rather than Highlighted.

Here's an example of how to change the backgroundColor and titleColor of a UIButton when it becomes focused. (Accomplished with the help of this answer):

import UIKit

class ViewController: UIViewController {

    let myButton = UIButton(type: UIButtonType.Custom)
    let myOtherButton = UIButton(type: UIButtonType.Custom)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Button we will change on focus
        myButton.frame = CGRectMake(view.frame.midX - 300, view.frame.midY, 400, 100)
        myButton.setTitle("myButton", forState: .Normal)

        // Normal
        myButton.backgroundColor = UIColor.whiteColor()
        myButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)

        // Focused
        myButton.setBackgroundImage(imageWithColor(UIColor.orangeColor()), forState: .Focused)
        myButton.setTitleColor(UIColor.whiteColor(), forState: .Focused)

        view.addSubview(myButton)

        // Other button so we can change focus // Just for example
        myOtherButton.frame = CGRectMake(view.frame.midX + 300, view.frame.midY, 400, 100)
        myOtherButton.setTitle("myOtherButton", forState: .Normal)

        // Normal
        myOtherButton.backgroundColor = UIColor.whiteColor()
        myOtherButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)

        // Focused
        myOtherButton.setBackgroundImage(imageWithColor(UIColor.orangeColor()), forState: .Focused)
        myOtherButton.setTitleColor(UIColor.whiteColor(), forState: .Focused)

        view.addSubview(myOtherButton)
    }

    // Function to create a UIImage filled with a UIColor
    func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRectMake(0, 0, 1, 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }
}

This example in action:




回答2:


This may help. Change the Button-Type: System to Custom (IB or programmatically), Follow image

changes behaviour of highlighted button.

Add this property in your method "adjustsImageWhenHighlighted = NO".



来源:https://stackoverflow.com/questions/34434636/uibutton-background-color-overlaps-text-on-highlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!