【问题标题】:How to create a button with a clear background如何创建具有清晰背景的按钮
【发布时间】:2014-03-11 03:12:56
【问题描述】:

如何创建一个除了图像之外透明的 UIButton?

我有透明背景的 PNG。我用它创建了一个按钮,然后我得到了蓝色的图像。

如果我将 tintColor 设置为 clearColor,色调会消失,但黑色图像也会消失。

我一直使用 UIButtonTypeSystem 作为按钮类型。

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem] ;
button.frame = rect ;
[button setImage:image forState:UIControlStateNormal] ;
button.backgroundColor = [UIColor clearColor] ; // Does nothing
button.tintColor = [UIColor clearColor] ;  // Makes the button go away.

【问题讨论】:

    标签: ios uibutton


    【解决方案1】:

    为了有一个透明的按钮,你应该把它设为自定义类型。所以你的代码会变成:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = rect ;
    [button setImage:image forState:UIControlStateNormal] ;
    button.backgroundColor = [UIColor clearColor] ; // This is not necessary, unless you want to specify a color - say [UIColor redColor] - for the button
    

    由于 UIButtonTypeCustom 的行为方式与 UIButtonTypeSystem 不同,因此您应该查看各种布尔标志来设置特定的特征。它们必须手动设置或通过 Interface Builder 设置。这是list 的属性。

    【讨论】:

      【解决方案2】:
      UIButton *button = [UIButton UIButtonTypeCustom] ;
      button.frame = rect ;
      [button setImage:image forState:UIControlStateNormal];
      

      你只需要使用UIButtonTypeCustom,不需要为UIButton设置任何backgroundColor或tintColor。

      【讨论】:

        【解决方案3】:

        您需要使用UIButtonTypeCustom

        【讨论】:

          【解决方案4】:

          只需更改一行使用 UIButtonTypeCustom 而不是 UIButtonTypeSystem

          UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom] ;
          button.frame = rect ;
          [button setImage:image forState:UIControlStateNormal] ;
          button.backgroundColor = [UIColor clearColor] ;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-12-12
            • 1970-01-01
            • 2011-03-13
            • 2012-11-29
            • 1970-01-01
            • 2019-02-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多