为什么其中一个段在分段控件上显示为禁用?

时间:2014-03-30 12:47:38

标签: ios uisegmentedcontrol

我有一个分段控件,可以显示段的自定义图像。我创建了如下

[repaymetSegmentedControl setBackgroundImage:[UIImage imageNamed:@"-SingleJointOffState"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[repaymetSegmentedControl setBackgroundImage:[UIImage imageNamed:@"-SingleJointOnState"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];


[repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"on_div"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"off_div"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

CGRect frameSeg= repaymetSegmentedControl.frame;
[repaymetSegmentedControl setFrame:CGRectMake(frameSeg.origin.x, frameSeg.origin.y-5, frameSeg.size.width, frameSeg.size.height + 15)];

在我运行应用程序时,我会根据偏好选择其中一个细分。它如图所示。不知道它有什么问题。

enter image description here

使用的图像如下

enter image description here

enter image description here

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:1)

viewDidLoad中使用此代码给了我一些我认为您正在寻找的结果(我将您的图片重命名为让我更清楚一点)。

    [repaymetSegmentedControl setBackgroundImage:[UIImage imageNamed:@"whiteBG"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [repaymetSegmentedControll setBackgroundImage:[UIImage imageNamed:@"pinkBG"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    [repaymetSegmentedControl setBackgroundImage:[UIImage imageNamed:@"pinkBG"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [repaymetSegmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal ];
    [repaymetSegmentedControll setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateHighlighted ];
    [repaymetSegmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected ];

    [repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"pinkDivider"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"pinkDivider"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"pinkDivider"] forLeftSegmentState:UIControlStateHighlighted rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"pinkDivider"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    [repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"whiteDivider"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

使用此代码,结果如下。附图:

enter image description here

enter image description here

答案 1 :(得分:0)

我的猜测是,所选片段的文字颜色设置为黑色和白色。因此,在屏幕截图中,选择了中间片段。

问题可能出在其他地方:

[repaymetSegmentedControl setBackgroundImage:[UIImage imageNamed:@"-SingleJointOffState"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[repaymetSegmentedControl setBackgroundImage:[UIImage imageNamed:@"-SingleJointOnState"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

让图像名称以-开头似乎很奇怪。

[repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"on_div"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[repaymetSegmentedControl setDividerImage:[UIImage imageNamed:@"off_div"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

您似乎缺少正确选择/左侧正常选项。

另外,由于您的设计看起来很平坦,我不确定您是否需要其中任何一种。

最后你能把所有的图像都附在问题上了吗?

答案 2 :(得分:0)

您的线段分割线图像应与背景图像的高度相同。