设置背景图像时,未设置UISegmentedControl段宽度

时间:2012-05-04 17:05:10

标签: objective-c ios cocoa-touch ios5 uisegmentedcontrol

我有一个分段控件,我希望第一个和最后一个项具有指定的宽度(比如,比其他更宽)。当我setWidth:forSegmentAtIndex:用于标准样式的segmentedControl(即[[UISegmantedControl appearence] setBackgroundImage:forState:barMetrics:]未设置)时,事情就像他们应该的那样。但是当我设置背景图像时,分段宽度不会改变。

这是我的代码:

[[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:@"btn_normal.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:@"btn_selected.png"]  stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"nn.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"sn.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"ns.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setContentMode:UIViewContentModeScaleToFill];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:0];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:4];

这样,我的所有片段的大小都会自动调整到相等的宽度 当我在上面注释掉所有内容时

[self.segmentedControl setContentMode:UIViewContentModeScaleToFill];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:0];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:4];

尺寸设置正确。
这是我的问题:如何使用背景图像设置分段的大小?
我是cocoa-touch和objective-c的新手,所以我可能会遗漏一些东西......
你能帮忙吗?

2 个答案:

答案 0 :(得分:6)

我认为当您使用 [UISegmentedControl外观] 代理时,它会覆盖“setWidth:forSegmentAtIndex:”方法值。所以可能的解决方案:

1)

[[UISegmentedControl appearance] setContentMode:UIViewContentModeScaleToFill];
[[UISegmentedControl appearance] setWidth:100.0 forSegmentAtIndex:0];
[[UISegmentedControl appearance] setWidth:100.0 forSegmentAtIndex:4];

我不建议使用它,因为它会为您应用中的每个分段控件设置0和4段的自定义宽度。

2)

[self.segmentedControl setBackgroundImage:[[UIImage imageNamed:@"btn_normal.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[[UIImage imageNamed:@"btn_selected.png"]  stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:[UIImage imageNamed:@"nn.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:[UIImage imageNamed:@"sn.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:[UIImage imageNamed:@"ns.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

如果您需要应用中的所有分段控件都应具有自定义背景,那么我建议您编写自己的代理方法。像这样:

- (UISegmentedControl *) customSegmentedControl {
    UISegmentedControl *segmentedControl = [ [ [UISegmentedControl alloc] init] autorelease];

    [segmentedControl setBackgroundImage: [ [UIImage imageNamed: @"btn_normal.png"] stretchableImageWithLeftCapWidth: 25 topCapHeight: 0] forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
    [segmentedControl setBackgroundImage: [ [UIImage imageNamed: @"btn_selected.png"] stretchableImageWithLeftCapWidth: 25 topCapHeight: 0] forState: UIControlStateSelected barMetrics: UIBarMetricsDefault];
    [segmentedControl setDividerImage: [UIImage imageNamed: @"nn.png"] forLeftSegmentState: UIControlStateNormal rightSegmentState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
    [segmentedControl setDividerImage: [UIImage imageNamed: @"sn.png"] forLeftSegmentState: UIControlStateSelected rightSegmentState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
    [segmentedControl setDividerImage: [UIImage imageNamed: @"ns.png"] forLeftSegmentState: UIControlStateNormal rightSegmentState: UIControlStateSelected barMetrics: UIBarMetricsDefault];

    return segmentedControl;
}

答案 1 :(得分:1)

我通过编写基于此http://idevrecipes.com/2010/12/11/custom-segmented-controls/

的自定义控件解决了这个问题