UIButton 背景颜色与突出显示的文本重叠 [英] UIButton background color overlaps text on highlight

查看:29
本文介绍了UIButton 背景颜色与突出显示的文本重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我为状态 highlighted 设置我的 UIButtonbackgroundColortextColor 后,将显示以下输出:

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

我在 tvOS 上也有很多问题,从 Interface Builder 设置背景和文本颜色(不适用于 State Config).我必须结合IB的属性和代码.

这是我的代码:

if shopNowButton.highlighted == true {shopNowButton.highlighted = falseshopNowButton.backgroundColor = UIColor.orangeColor()shopNowButton.setTitleColor(UIColor.whiteColor(), forState: .Highlighted)}别的 {shopNowButton.highlighted = trueshopNowButton.backgroundColor = UIColor.whiteColor()shopNowButton.layer.borderWidth = 1shopNowButton.layer.borderColor = UIColor.orangeColor().CGColorshopNowButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)}

解决方案

我相信

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

解决方案

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:

这篇关于UIButton 背景颜色与突出显示的文本重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆