问题
I am having difficulty working with/understanding the resizableImageWithCapInsets
I have setup a button as follows:
facebookLoginButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 300,50)];
facebookLoginButton.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIImage *facebookButton = [[UIImage imageNamed:@"FacebookLogin"]
resizableImageWithCapInsets:UIEdgeInsetsMake(20,90,20,56)];
[facebookLoginButton setBackgroundImage:facebookButton forState:UIControlStateNormal];
[facebookLoginButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[facebookLoginButton setTitle:NSLocalizedString(@"FacebookLoginButton", "Facebook Login Button") forState:UIControlStateNormal];
[facebookLoginButton setTitleShadowColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.35] forState:UIControlStateNormal];
[facebookLoginButton addTarget:self action:@selector(facebookLoginButton:) forControlEvents:UIControlEventTouchUpInside];
As you can see above the frame for the button view is 300x50 pixels. I have created an image for retina (then will scale down later).
The image is a png created in PS, sized 144x100px. That is double the height of the frame (for uibutton) and the width necessary to have the Facebook F, with space. The idea is to have the F stay static on the left and the stretchable part as highlighted below.
Here is the image itself if that helps;
Here is the current outcome:
I am trying to find out the correct insets to enter. I have searched through a load of tutorials on this but none have explained enough for me to understand how the insets actually work. If anyone could explain this or link to a tutorial on how they are calculated that would be even better.
I have currently set the top and bottom as 0 as in theory they do not need repeating, just a static left larger than the static right. The inset for the right is only for the rounded borders and the middle part needs repeating.
回答1:
If you using image for retina make sure that you postfixed name with @2x
.
Also make note that you need to specify intents in units (not pixels).
UIImage *facebookButton = [[UIImage imageNamed:@"FacebookLogin"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,40,0,10)];
来源:https://stackoverflow.com/questions/15713799/image-not-resizing-correctly-using-resizableimagewithcapinsets