简体   繁体   English

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

[英]UISegmentedControl segment width of is not set when the background image is set

I have a segmented control and I want first and last items to be of specified width (say, wider than others). 我有一个分段控件,我希望第一个和最后一个项具有指定的宽度(比如,比其他更宽)。 When I setWidth:forSegmentAtIndex: for standard-styled segmentedControl (ie [[UISegmantedControl appearence] setBackgroundImage:forState:barMetrics:] not set), things behave themselves as they should. 当我setWidth:forSegmentAtIndex:对于标准样式的segmentedControl(即[[UISegmantedControl appearence] setBackgroundImage:forState:barMetrics:] not set),事情就像他们应该的那样。 But when I set background image, segments width doesn't change. 但是当我设置背景图像时,分段宽度不会改变。

Here is my code: 这是我的代码:

[[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];

With this, all my segments appear automatically sized to the equal width. 这样,我的所有片段的大小都会自动调整到相等的宽度。
And when I comment out all above this 当我在上面评论这一切时

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

the sizes are set correctly. 尺寸设置正确。
Here is my question: how can I set sizes for the segments with background images? 这是我的问题:如何使用背景图像设置片段的大小?
I'm new to cocoa-touch and objective-c, so I might be missing something... 我是cocoa-touch和objective-c的新手,所以我可能会遗漏一些东西......
Could you help please? 你能帮帮忙吗?

I think when you use [UISegmentedControl appearance] proxy it overrides "setWidth:forSegmentAtIndex:" method values. 我认为当你使用[UISegmentedControl外观]代理时,它会覆盖“setWidth:forSegmentAtIndex:”方法值。 So possible solutions: 可能的解决方案:

1) 1)

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

I would not recommend to use this because it will set custom width for 0 and 4 segments for every segmented control in your app. 我不建议使用它,因为它会为应用程序中的每个分段控件设置0和4段的自定义宽度。

2) 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];

If you need that all segmented controls in your app should have custom background then I would recommend to write your own proxy method. 如果您需要应用程序中的所有分段控件都应具有自定义背景,那么我建议您编写自己的代理方法。 Something like this: 像这样的东西:

- (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;
}

我通过编写自定义控件解决了这个问题,基于此http://idevrecipes.com/2010/12/11/custom-segmented-controls/

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

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