【问题标题】:customly set button background color ios自定义设置按钮背景颜色ios
【发布时间】:2016-03-02 06:39:13
【问题描述】:

我正在尝试将 UIButton 的背景颜色更改为自定义颜色。所有默认的灰色、蓝色等都不够。

    UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
    _button.backgroundColor = [UIColor myColor]; 

第二行出现错误提示

No known class method for selector 'myColor'

【问题讨论】:

    标签: ios objective-c user-interface colors


    【解决方案1】:

    你的代码没问题,简单的错误是你又在第二行创建了[UIColor property]

    UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
    

    不像

      _button.backgroundColor = [UIColor myColor]; 
    

    喜欢

      _button.backgroundColor = myColor; 
    

    更新

    你可以直接使用color属性,比如

       _button.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f]; 
    

    【讨论】:

      【解决方案2】:

      只需使用以下代码更改您当前的代码

      UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
      _button.backgroundColor = myColor;  // because myColor is UIColor
      

      【讨论】:

        【解决方案3】:

        试试这个:

         UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
         _button.backgroundColor = myColor;
        

        myColor 未在 UIColor 中定义。这是您定义的变量。

        【讨论】:

          【解决方案4】:

          使用起来更方便:

           _button.backgroundColor = [UIColor colorWithRed:1.0/255.0 green:1.0/255.0 blue:0.0/255.0 alpha:1.0f];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-12-14
            • 2020-01-06
            • 1970-01-01
            • 2021-10-29
            • 1970-01-01
            • 1970-01-01
            • 2021-05-10
            相关资源
            最近更新 更多