简体   繁体   English

UISegmentedButton使用图案设置色调颜色

[英]UISegmentedButton setting tint color using pattern

So I have the following code: 所以我有以下代码:

  UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 150, 35)];
    [segmentedCtrl setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"duck.png"]]];


 [segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBar];

And here's the duck image I am trying to use: 这是我要使用的鸭子图像:

在此处输入图片说明

Anybody care to tell me why it's not working. 有人在乎告诉我为什么它不起作用。 I tried with any kind of image and it's not working. 我尝试使用任何类型的图像,但均无法正常工作。 FYI Iam not trying to have a duck background in my segmented control. 仅供参考,我不是想在我的分段控件中使用鸭子背景。 This is just to illustrate the point that setTint using the colorWithPatternImage doesn't work for any kind of image. 这只是为了说明一点,使用colorWithPatternImage的setTint不适用于任何类型的图像。

have you tried a 1px width image? 您是否尝试过1px宽的图片? The image you are using is too large for pattern creation 您使用的图像太大,无法创建图案

UISegmentedControl (as all other UIKit controls, I think) doesn't support pattern colors as tint colors. UISegmentedControl (与其他所有UIKit控件一样,我认为)不支持将图案颜色用作色调颜色。 The tint color is basically used to create a glossy gradient and that would have to be done very differently with an arbitrary pattern. 色调颜色基本上用于创建光泽渐变,并且必须使用任意图案进行非常不同的处理。

To customize the appearance of the background, use setBackgroundImage:forState:barMetrics: and setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics: instead (with appropriate images of course, patterns aren't a good fit for these). 要自定义背景的外观,请使用setBackgroundImage:forState:barMetrics:setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics:当然,使用适当的图像,图案不太适合这些背景)。

The tintColor on UISegmentedControl is only used as the basis to calculate the gradient used in the control. UISegmentedControl上的tintColor仅用作计算控件中使用的渐变的基础。 You can't very well have a gradient image, so it's just using part of it. 您不太可能拥有渐变图像,因此仅使用了其中的一部分。 It sounds like what you need is a custom subclass of UISegmentedControl. 听起来您需要的是UISegmentedControl的自定义子类。

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
    [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"up_button.png"] atIndex:0 animated:YES];
    [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"down_button.png"] atIndex:1 animated:YES];

First take the RGB value of color by opening your image in Photochop , then use that values. 首先通过在Photochop中打开图像来获取颜色的RGB值,然后使用该值。

segment-control-background-color 段控制背景色

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM