gpt4 book ai didi

iOS - 可以拉伸(stretch) UIImageView 但不能拉伸(stretch) UIButton?

转载 作者:可可西里 更新时间:2023-11-01 04:19:40 27 4
gpt4 key购买 nike

看:

//UIImageView* stretchTest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
//[self addSubview:stretchTest];


UIButton *stretchTest = [UIButton buttonWithType:UIButtonTypeCustom];
[stretchTest setFrame:CGRectMake(0, 0, 400, 100)];
[stretchTest setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[self addSubview:stretchTest];

stretchTest.contentStretch = CGRectMake(0.5, 0.5, 0, 0);
stretchTest.contentMode = UIViewContentModeScaleToFill;

CGRect frame = stretchTest.frame;
frame.size.height = 300;
stretchTest.frame = frame;

使用 UIImageView(上面已注释掉),图像会适本地拉伸(stretch) - 圆角会保持正确的半径,因为只有图像的中心像素会被拉伸(stretch)。

使用 UIButton 时,图像会被错误地拉​​伸。拐角半径没有保持不变,变得很难看。

UIImageView 和 UIButton 都是 UIView 的子类。为什么按钮调整大小与 imageView 不同?

最佳答案

您正在对 UIButton 的工作方式做出假设。它的实现方式与 UIImageView 不同。 UIImageView 只是一个 View ,没有 subview ,它的内容是图像。 UIButton 不同,它的工作方式是私有(private)实现细节。

如果您尝试在按钮上适本地拉伸(stretch)图像,您应该使用 -[UIImage stretchableImageWithLeftCapWidth:topCapHeight:] 来获取知道应该如何拉伸(stretch)的 UIImage。如果你只想拉伸(stretch)中间的像素,你可以使用类似的东西

UIImage *image = [UIImage imageNamed:@"image.png"];
image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];

关于iOS - 可以拉伸(stretch) UIImageView 但不能拉伸(stretch) UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086065/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com