UIButton背景颜色与高亮显示的文本重叠

时间:2015-12-23 11:25:41

标签: swift uibutton tvos

我为状态UIButton设置backgroundColor textColorhighlighted后,会显示以下输出:

enter image description here

这里的问题是按钮的背景与白色文本重叠。我该如何解决这个问题?

我在tvOS上遇到很多问题,从Interface Builder设置背景和文本颜色(不在State Config下工作)。我必须结合IB的属性和代码。

这是我的代码:

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)
}

2 个答案:

答案 0 :(得分:2)

这可能会有所帮助。 将按钮类型:系统更改为自定义(IB或以编程方式), Follow image

更改突出显示按钮的行为。

在您的方法中添加此属性" adjustsImageWhenHighlighted = NO"。

答案 1 :(得分:2)

我相信您实际需要的UIControlStateFocused而不是Highlighted

以下是一个示例,说明如何在焦点变为backgroundColor时更改titleColorUIButton。 (在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
    }
}

这个例子在行动:

enter image description here