UISegmentedControl分割器图像未显示

时间:2012-10-31 23:18:39

标签: objective-c ios cocoa-touch uisegmentedcontrol

我有UISegmentedControl,我正在尝试设置分频器图像。图像为黑色,宽约2px。

UIImage *divider = [UIImage imageNamed:@"ZSDivider.png"];
[self.segmentedControl setDividerImage:divider forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:divider forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:divider forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

我不希望分频器看起来像什么,只是每个州的黑线,但它根本没有出现。

任何想法我做错了什么?

2 个答案:

答案 0 :(得分:1)

分割器图像的高度与分割控件的高度相同吗?如果我没记错,如果分隔符与控制高度不匹配,UISegmentedControl会出现问题。

答案 1 :(得分:0)

您是否确定“divider”和“self.segmentedControl”不为空?

这看起来是错误检查的绝佳机会:

UIImage *divider = [UIImage imageNamed:@"ZSDivider"];
if(divider)
{
    if(self.segmentedControl)
    {
        [self.segmentedControl setDividerImage:divider forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self.segmentedControl setDividerImage:divider forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self.segmentedControl setDividerImage:divider forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    } else {
        NSLog( @"segmentedControl is NULL");
    }
} else {
    NSLog( @"divider image is NULL");
}