1

我正在使用 Rubymotion 创建一个 iOS 应用程序。在其中一个视图中,我使用的是 UIsegmentedcontroller,它工作正常。

我该如何自定义它。我需要为整个背景使用自定义背景图像(这不起作用)和每个按钮的图像(这有效)。

这是我当前的代码:

options = ["Selected","Unselected"]
    segment = UISegmentedControl.alloc.initWithItems(options)
    segment.addTarget(self, action: :'open:', forControlEvents:UIControlEventValueChanged)
    segment.frame = [[0, 0],[320, 40]]
    segment.segmentedControlStyle = 7
    segment.selectedSegmentIndex = 0
    segment.setImage(button, forSegmentAtIndex:0)
    segment.setImage(button2, forSegmentAtIndex:1)

我试过这段代码:

segment.apperance.setBackgroundImage(button, forState:UIControlStateNormal , forBarMetrics:UIBarMetricsDefault)

segment.setBackgroundImage(button, forState:UIControlStateSelected , forBarMetrics:UIBarMetricsDefault)

但如果我这样做,我会收到此错误:

undefined method `appearance' for #<UISegmentedControl:0x9655a90> (NoMethodError)

我也尝试过没有外观,但后来我得到了:

undefined method `setBackgroundImage' for #<UISegmentedControl:0x946db70> (NoMethodError)

更新

我试过了,背景上没有显示图像:

segment.setBackgroundImage(background, forState:UIControlStateNormal, barMetrics:UIBarMetricsDefault)
4

1 回答 1

2

利用 setBackgroundImage:forState:barMetrics:

它为给定的状态和条形指标设置背景图像。

您可以在Apple.Developer 文档中查看详细信息

示例代码:

取自这里

[[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
于 2012-12-06T13:29:38.990 回答