【问题标题】:Setting a background image for UIButton为 UIButton 设置背景图像
【发布时间】:2011-01-05 19:40:53
【问题描述】:

我是 Objective C 的新手。如何将此按钮的背景设置为图像(图像已经在 XCode 的资源文件夹中,“blue_button.png”)而不是 clearColor?另外,我更喜欢使用图像作为按钮的形状,而不是 UIButtonTypeRoundedRect。

btnClear = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

btnClear.frame = CGRectMake(115, 350, 90, 40);

[btnClear setTitle:@"Clear" forState:UIControlStateNormal];

btnClear.backgroundColor = [UIColor clearColor];

[btnClear addTarget:self action:@selector(clearAction:) 

forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnClear];

我知道如何在 Interface Builder 中执行此操作,但我更愿意学习在 XCode 中执行此操作。

【问题讨论】:

  • 如果你使用 nib,你可以在 Interface Builder 中做到这一点
  • 你应该接受一个答案,这对所有参与者都有好处......

标签: iphone objective-c xcode


【解决方案1】:

这行得通:

UIButton *btnClear = [[UIButton alloc] init];
btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btnClear.frame = CGRectMake(115, 200, 90, 40);
[btnClear setTitle:@"Clear" forState:UIControlStateNormal];
[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];
[btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnClear];

【讨论】:

    【解决方案2】:

    UIButton class reference-setBackgroundImage:forState:

    如果您不想使用圆角样式,请使用 UIButtonTypeCustom 类型而不是 UIButtonTypeRoundedRect 创建您的按钮。

    【讨论】:

      【解决方案3】:

      您需要使用以下 UIButtonTypeCustom 类型声明您的按钮:

      btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
      

      那么您应该可以使用以下内容:

      [btnClear setImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];
      

      可以设置6种不同的控制状态。

      enum {
         UIControlStateNormal               = 0,
         UIControlStateHighlighted          = 1 << 0,
         UIControlStateDisabled             = 1 << 1,
         UIControlStateSelected             = 1 << 2,
         UIControlStateApplication          = 0x00FF0000,
         UIControlStateReserved             = 0xFF000000
      };
      

      以下是一些可能有帮助的参考资料:

      UIButton Class Reference

      UIControl Class Reference

      【讨论】:

      • 这确实有效,但我现在不确定如何设置标题。我正在使用 [btnClear setTitle:@"Clear" forState:UIControlStateNormal];
      • 看看这个,看看它是否有帮助。 stackoverflow.com/questions/1469474/…
      • 您可以设置图像或平铺,但您可以做一些事情来显示标题
      【解决方案4】:

      您可以使用[UIColor colorWithPatternImage:] 为图像设置背景颜色。要使用图像,您应该将按钮样式更改为自定义。

      【讨论】:

        【解决方案5】:

        如果你想用互联网上的图片设置按钮背景,使用SDWebImage 是一个优雅的选择:

        #import <SDWebImage/UIButton+WebCache.h>
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [button setBackgroundImageWithURL:imageURL forState:UIControlStateNormal];
        

        【讨论】:

          【解决方案6】:

          无需将按钮设置为UIButtonTypeCustom - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state 方法也适用于UIButtonTypeRoundedRect

          【讨论】:

            【解决方案7】:

            斯威夫特 4+

            var btnClear = UIButton()
            btnClear = UIButton(type: .custom)
            btnClear.frame = CGRect(x: 115, y: 200, width: 90, height: 40)
            btnClear.setBackgroundImage(UIImage(named: "blue_button.png"), for: .normal)
            view.addSubview(btnClear)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-09-19
              • 1970-01-01
              • 1970-01-01
              • 2019-10-13
              • 1970-01-01
              相关资源
              最近更新 更多