title: How to add CAGradientLayer to UIButton
body: <p>I am trying to modify a button's look and spent a long while trying to figure out why I can not apply a gradient to it like with this simple code below:</p>
  
  <pre><code>[self.playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  self.playButton.backgroundColor = [UIColor blackColor];
  
  CAGradientLayer *gradient = [[CAGradientLayer alloc] init];
  gradient.frame = self.playButton.bounds;
  gradient.position = CGPointMake(self.playButton.bounds.size.width/2, self.playButton.bounds.size.height/2);
  gradient.colors = [NSArray arrayWithObjects:
                     (id)[UIColor greenColor].CGColor,
                     (id)[UIColor redColor].CGColor,
                     nil];
  gradient.locations = [NSArray arrayWithObjects:
                        [NSNumber numberWithFloat:0.0f],
                        [NSNumber numberWithFloat:1.0f],
                        nil];
  
  [self.playButton.layer addSublayer:gradient];
  </code></pre>
  
  <p>It executes, but nothing happens. I know the code ought to work, because many other use it and when changing the <code>self.playButton</code> to say <code>self.view</code> then it adds a beautiful red-green gradient layer to the <code>UIView</code>.</p>
  
  <p>It may be worth noting that I the custom button is defined as an <code>IBOutlet</code> like this:</p>
  
  <pre><code>@property (weak, nonatomic) IBOutlet UIButton *playButton;
  </code></pre>
  
  <p>Any help is greatly appreciated.</p>
  
  <p><strong>UPDATE &amp; FIX</strong></p>
  
  <p>When changing the button to use a scalable image instead of a gradient, I forgot to re-comment-out the gradient code (after trying <a href="https://stackoverflow.com/users/653513/rokjarc">rokjarc</a>'s idea) and when changing the button's <code>imageView.content</code> to <code>UIViewContentModeScaleToFill</code> the gradient displayed...</p>
  
  <pre><code>self.playButton.imageView.contentMode = UIViewContentModeScaleToFill;
  </code></pre>
  
  <p>So thanks <a href="https://stackoverflow.com/users/653513/rokjarc">rokjarc</a> I suppose :D</p>