【问题标题】:UIButton custom backgroundColor in tvOStvOS 中的 UIButton 自定义背景颜色
【发布时间】:2016-04-07 19:43:20
【问题描述】:

我的TVos 应用程序中有一个UIButton,我想在其中更改backgroundColor 以及titleLabel 的字体。

当按钮获得焦点时,文本、字体和backgroundColor 应该相同,但应该显示它变成focused view

我的ViewDidLoad 中有这三行代码:

   orderButton.titleLabel?.font = UIFont(name: "RobotoCondensed-Regular", size: 40)
   orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: [.Focused, .Normal])
   orderButton.setTitleColor(UIColor.whiteColor(), forState: [.Focused, .Normal])

问题

当我的视图被加载时,我的按钮仍然是默认的灰色。当它获得焦点时,它会获得我在ViewDidLoad 中设置的颜色。

有什么帮助吗?

【问题讨论】:

    标签: ios swift uibutton swift2 tvos


    【解决方案1】:

    你必须分别指定两个状态,像这样

     orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: .Focused)
     orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: .Normal)
    

    正如文档所说

      func setBackgroundImage(_ image: UIImage?,forState state: UIControlState)
    

    我们一次只能指定一个状态

    【讨论】:

    • 成功了。但是你不能以任何方式在一条线上做到这一点吗?
    • 就文档所说的 func setBackgroundImage(_ image: UIImage?, forState state: UIControlState) 我们每次只有一个状态可以指定,
    【解决方案2】:

    您可以重写 didUpdateFocusInContext 方法来实现这一点。 假设您的按钮已将标签设置为 1。现在您可以执行以下操作:

         override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    
            if let nextFocusedView = context.nextFocusedView {
              if button.tag == 1 {
           // Change the font and background color here for Focused state
                }
            }
    
        if let previousFocusView = context.previouslyFocusedView {
             if button.tag == 1 {
         // Change the font and background color here for Non-Focused state
             }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2018-06-29
      • 2011-09-29
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多