iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom

前端 未结 15 1318
有刺的猬
有刺的猬 2020-12-01 13:45

i want to change the button color when it is clicked. I used :

[button setBackgroundColor:[UIColor redColor]];

but this shows red color onl

相关标签:
15条回答
  • 2020-12-01 14:17

    Make the button type as custom.

    Eg

    button = [UIButton buttonWithType:UIButtonTypeCustom];
    

    All the best.

    0 讨论(0)
  • 2020-12-01 14:19

    Just an update to Hendrik answer.

    To work well with Auto Layout you need to set UIView frame size to button intrinsicContentSize, otherwise it will screw up layout.

    - (void)setColor:(UIColor *)color forState:(UIControlState)state
    {
        UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.intrinsicContentSize.width, self.intrinsicContentSize.height)];
        colorView.backgroundColor = color;
    
        UIGraphicsBeginImageContext(colorView.bounds.size);
        [colorView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        [self setBackgroundImage:colorImage forState:state];
    }
    
    0 讨论(0)
  • 2020-12-01 14:22

    I believe a more efficient solution than any of the ones listed is to just draw the image in Core Graphics directly. You avoid having to make a UIView and calling renderInContext:, which can be pretty slow.

    + (UIImage *) imageWithColor:(UIColor *)color
    {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    

    You can use this in a UIImage category to create an image to use in a UIButton.

    0 讨论(0)
  • 2020-12-01 14:24

    Thanks to @Dima

    Here is an updated code for swift 3.0 as UIButton extension

    extension UIButton {
      // thanks to @Dima http://stackoverflow.com/a/24665476/2581637 update code for swift 3
      func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
        func imageWithColor(color: UIColor) -> UIImage? {
          let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
          UIGraphicsBeginImageContext(rect.size)
          if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(color.cgColor)
            context.fill(rect)
          }
    
          let image = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
    
          return image
        }
    
        self.setBackgroundImage(imageWithColor(color: color), for: state)
      }
    }
    
    0 讨论(0)
  • 2020-12-01 14:24
     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    
     [button1 setBackgroundColor:[UIColor redColor]];
    
    0 讨论(0)
  • 2020-12-01 14:29

    Make sure the button type is custom as someone else already stated. Then doing the following will bring back those rounded corners:

     [self.myButton.layer setCornerRadius:8.0f];
     [self.myButton.layer setMasksToBounds:YES];
     [self.myButton.layer setBorderWidth:1.0f];
     [self.myButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
     self.myButton.backgroundColor = [UIColor redColor];
    

    Though, personally, I'm a big fan of gradient buttons over solid colors... also, background color doesn't have different states, whereas background images do:

     [self.myButton setBackgroundImage:[UIImage imageNamed:@"gradient.png"] forState:UIControlStateNormal];
    

    Using an image, your button will darken with selection (or you could provide a different background image per state to do something different). This won't happen with background color, the background will always stay the same, only your button label changing per state.

    0 讨论(0)
提交回复
热议问题