所以我有一个 1px 宽度和 50px 高度的 .png 文件,我想在 UISegmentedButton 中水平平铺它,所以我做了:
[segmentedCtrl setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"btn-gradient-brown.png"]]];
然而,我得到的只是黑色。这是为什么以及如何正确执行此操作?这是我拥有的 1px,如果它有助于澄清我的观点
所以我有一个 1px 宽度和 50px 高度的 .png 文件,我想在 UISegmentedButton 中水平平铺它,所以我做了:
[segmentedCtrl setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"btn-gradient-brown.png"]]];
然而,我得到的只是黑色。这是为什么以及如何正确执行此操作?这是我拥有的 1px,如果它有助于澄清我的观点
您可以尝试使用核心图形方法进行平铺,从this question看这段代码,
CGSize imageViewSize = imageView.bounds.size;
UIGraphicsBeginImageContext(imageViewSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextDrawTiledImage(imageContext, (CGRect){ CGPointZero, imageViewSize }, tileImage);
UIImage *finishedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
它创建一个带标题的图像。您可以将图像设置为 UIcontrol 的背景。
编辑:
tintColor
是创建分段控制默认渐变的基础。因此,将棕色渐变图像设置为色调颜色会为您提供黑色渐变。要设置渐变颜色,
uisegmentcontrol
类和覆盖drawRect
方法。backgroundImage
并设置适当的图像插图。