gpt4 book ai didi

objective-c - UISegmentedControl segment width of 设置背景图片时未设置

转载 作者:可可西里 更新时间:2023-11-01 17:10:26 30 4
gpt4 key购买 nike

我有一个分段控件,我希望第一个和最后一个项目具有指定的宽度(例如,比其他项目更宽)。当我 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 的新手,所以我可能遗漏了一些东西......
你能帮忙吗?

最佳答案

我认为当您使用[UISegmentedControl appearance] 代理时,它会覆盖"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;
}

关于objective-c - UISegmentedControl segment width of 设置背景图片时未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10453172/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com