【问题标题】:UIButton background color overlaps text on highlightUIButton 背景颜色与突出显示的文本重叠
【发布时间】:2015-12-23 11:25:41
【问题描述】:

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

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

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

【问题讨论】:

  • button.setTitleColor(UIColor.whiteColor(), forState: .Selected)
  • 您是否为按钮的 TouchUpInside 事件添加了此代码?
  • 按钮不会被触摸。它只需要在突出显示时更改其背景颜色。
  • @TeodorCiuraru 按钮不会被触摸,那么这个UIButton应该是UILabel吗?
  • 是的。使用 UILabel 它可以工作,但是对于下一页,我仍然需要一个具有该功能的 UIButton。

标签: swift uibutton tvos


【解决方案1】:

这可能会有所帮助。 将 Button-Type: System 更改为自定义(IB 或以编程方式), Follow image

改变高亮按钮的行为。

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

【讨论】:

    【解决方案2】:

    我相信您实际寻找的UIControlStateFocused,而不是Highlighted

    这是一个示例,说明如何在 UIButton 成为焦点时更改 backgroundColortitleColor。 (在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
        }
    }
    

    这个例子在行动:

    【讨论】:

      猜你喜欢
      • 2020-10-23
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      相关资源
      最近更新 更多