Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing resizableImageWithCapInsets: image for button only works for the state set, other states show a "gap"
    primarykey
    data
    text
    <p>When using resizableImageWithCapInsets: to create an image for a UIButton only the normal state (the state set the image with using setBackgroundImage:forState:) works. All other states show a gap instead of the drawn image. UIButton says that if no image is set for a particular state, the normal state image will be used with an overlay for disabled and selected states.</p> <p>Here is the normal state:</p> <p><img src="https://i.stack.imgur.com/ygUxI.png" alt="enter image description here"></p> <p>Here is the selected state:</p> <p><img src="https://i.stack.imgur.com/varbO.png" alt="enter image description here"></p> <p>And here is the source image:</p> <p><img src="https://i.stack.imgur.com/qZszP.png" alt="enter image description here"></p> <p>It clearly is using the resizable image I provided, but the image is not drawing the resized area. (You can see the left and right edges but the middle area that is to be stretched just isn't drawn).</p> <p>Interestingly, stretchableImageWithLeftCapWidth:topCapHeight: does work. Now this is a deprecated method in iOS 5, but with the gap being shown in the new API, I may be stuck using it.</p> <p>I do recognize that I can provide more images for each state but that defeats the purpose I'm trying to achieve of reducing memory footprint plus adds extra dependency on my graphics designer which I'd like to avoid.</p> <pre><code>// This is the gist of the code being used UIImage* image = [UIImage imageNamed:@"button.png"]; UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2); image = [image resizableImageWithCapInsets:insets]; [self.button setBackgroundImage:image forState:UIControlStateNormal]; // Even doing the following results in the same behaviour [self.button setBackgroundImage:image forState:UIControlStateSelected]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload