简体   繁体   English

UIButton backgroundImage stretchableImageWithLeftCapWidth:topCapHeight问题

[英]UIButton backgroundImage stretchableImageWithLeftCapWidth:topCapHeight issue

I have a UIButton with a specified width of 200. Its autoresizingMask property is set to UIViewAutoresizingFlexibleWidth . 我有一个指定宽度为200的UIButton 。它的autoresizingMask属性设置为UIViewAutoresizingFlexibleWidth A UIImage is applied to this button's backgroundImage property. UIImage应用于此按钮的backgroundImage属性。 This UIimage is defined as follows: UIimage定义如下:

[[UIImage imageNamed:@"buttonimage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]

buttonimage.png has a width of 400px (2x width of button, because of retina resolution) and represents a rounded rectangle. buttonimage.png的宽度为400px(由于视网膜分辨率,按钮宽度为2 buttonimage.png )并且表示圆角矩形。 In portrait mode, everything is fine. 在纵向模式下,一切都很好。 As soon as the user rotates the device and the iPhone enters landscape mode, the UIButton is stretched. 一旦用户旋转设备并且iPhone进入横向模式, UIButton就会被拉伸。 Due to this behaviour, the left rounded border of the image stays the same ( stretchableImageWithLeftCapWidth: ) but the right corners are also stretched. 由于此行为,图像的左圆角边界保持不变( stretchableImageWithLeftCapWidth:但右角也被拉伸。 Is there any property I forgot to set that ensures only one specified pixel (eg the tenth one) is stretched and anything else retains it dimensions? 是否有任何我忘记设置的属性,确保只有一个指定的像素(例如第十个像素)被拉伸而其他任何东西都保留了它的尺寸?

Thanks in advance! 提前致谢!

Edit: If I use a smaller image, that is already stretched in portrait mode, both borders seem to be expanded. 编辑:如果我使用较小的图像,已经在纵向模式下拉伸,两个边框似乎都会被展开。

Solved! 解决了! If your image is called "myImage.png" and it is the retina version, just call it "myImage@2x.png" 如果您的图像被称为“myImage.png”并且它是视网膜版本,请将其命名为“myImage@2x.png”

Is there any property I forgot to set that ensures only one specified pixel (eg the tenth one) is stretched and anything else retains it dimensions? 是否有任何我忘记设置的属性,确保只有一个指定的像素(例如第十个像素)被拉伸而其他任何东西都保留了它的尺寸?

Not really, but given the size of your images, you should re-read the leftCapWidth property. 不是真的,但考虑到图像的大小,您应该重新读取leftCapWidth属性。

The way stretchable images work is: you provide the left cap, the next pixel is stretched, and the right side of the image with width = (total width - left cap + 1) stays the same. 可伸展图像的工作方式是:提供左上角,下一个像素被拉伸,图像右侧width = (total width - left cap + 1)保持不变。 Given that you are setting left cap to 10, and the original image is 400, you are telling iOS that the right non stretchable right side of your image is 400-10-1=389px . 鉴于您将左上角设置为10,原始图像为400,您告诉iOS图像右侧不可伸展的右侧是400-10-1=389px The same thing applies vertically. 同样的事情垂直适用。 Check if you are using @2x images on a normal device without a @2x on its name, or if the 2x versions don't have exactly twice the pixels, or there is an uppercase/lowercase difference. 检查您是否在普通设备上使用@ 2x图像,其名称上没有@ 2x,或者2x版本的像素不是两倍,或者是大写/小写差异。 You can find this out nslogging the size of the image loaded. 您可以通过nslogging找到加载的图像大小。

I don't know why the right side of your image is stretched. 我不知道为什么图像的右侧会被拉伸。 Given that you have height=0 , the whole image can be stretched vertically if the button height changes, but it's unlikely that this causes a distortion of only the right side. 假设你有height=0 ,如果按钮高度改变,整个图像可以垂直拉伸,但这不太可能导致只有右侧的扭曲。

There is nothing wrong with your code as far as I can tell, that should be causing the right side of the image to be cropped. 据我所知,你的代码没有任何问题,应该导致图像的右侧被裁剪。 Below is the exact code I use, which I know works, to produce the same effect you're looking for. 下面是我使用的确切代码,我知道它可以产生与你正在寻找相同的效果。

UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 245, 51)];
button.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[button setBackgroundImage:[[UIImage imageNamed:@"userbubble.png"] stretchableImageWithLeftCapWidth:40 topCapHeight:0] forState:UIControlStateNormal];
[self.view addSubview:button];

My guess is that the problem is coming from the png you're using, or possibly from the Compress PNG Files setting in your project. 我的猜测是问题来自您正在使用的png,或者可能来自项目中的Compress PNG Files设置。 Also, since the image you are using is so big, try putting the left cap out further, say 40 or 50 pixels in. 此外,由于您使用的图像太大,请尝试进一步放置左边的边框,例如40或50像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 stretchableImageWithLeftCapWidth:topCapHeight在initWithCoder中不起作用:UIImageView子类 - stretchableImageWithLeftCapWidth:topCapHeight doesn't work in initWithCoder: of UIImageView subclass 如何使用-(UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight将图像裁剪成碎片 - how to crop image in to pieces using - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight UIButton上的backgroundImage错误地缩放 - backgroundImage on UIButton scales wrongly 如何检查是否添加了UI按钮的backgroundimage - how to check if backgroundimage of UIbutton is added or not 我如何比较UIButton backgroundImage标题? - How would I compare UIButton backgroundImage title? 在UITabBar上选择时更改UIButton backgroundImage - Changing UIButton backgroundImage when selected on a UITabBar UIButton的BackgroundImage有时不会随着touchUpInside事件而改变 - BackgroundImage of UIButton sometimes did not change with touchUpInside event 如何在单击同一UIbutton的同时更改UIButton backgroundimage? - How to change UIButton backgroundimage while clicking same UIbutton? UIImage StretchableImageWithLeftCapWidth - UIImage stretchableImageWithLeftCapWidth UIButton和UIControlEventState问题 - UIButton and UIControlEventState issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM