0

I have added a UISlider control to an iPhone View-based application and have wired up a text field that gets the values from the slider. To fit with the user interface colors in the application I need to change the color of the slider.

With a little googling I was able to find 2 samples which do this (http://blog.hill-it.be/post/2009/03/23/Pimp-My-UISlider and http://developer.apple.com/iphone/library/samplecode/UICatalog/index.html) but I'm stuck because I'm not sure where I should be calling the code from.

I have a method which should return a custom UISlider object, but how do I override the one that I created through interface builder that lives in my sliderViewController.m and sliderViewController.h?

- (UISlider *)createCustomSlider
{
  CGRect frame = CGRectMake(174, 12.0, 120.0, 7.0);
  UISlider customSlider = [[UISlider alloc] initWithFrame:frame];
  [customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
  // in case the parent view draws with a custom color or gradient, use a transparent color
  customSlider.backgroundColor = [UIColor clearColor];  
  UIImage *stetchLeftTrack = [[UIImage imageNamed:@"orangeslide.png"]
                            stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
  UIImage *stetchRightTrack = [[UIImage imageNamed:@"yellowslide.png"]
                             stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
  [customSlider setThumbImage: [UIImage imageNamed:@"slider_ball.png"]     forState:UIControlStateNormal];
  [customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
  [customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
  customSlider.minimumValue = 0.0;
  customSlider.maximumValue = 100.0;
  customSlider.continuous = YES;
  customSlider.value = 50.0;

  return customSlider;
}

With .NET I'd normally have some code generated by the WinForms editor which I would then be able to override by creating a new object and assigning to the existing reference.

4

1 回答 1

1

你不应该这样做。你有三个选择:

  1. 与其让您的方法返回一个新的 UISlider 对象,不如让它修改您在 IB 中创建的现有 UISlider 实例。
  2. 编写一个自定义 UISlider 子类并将您的自定义代码放入其awakeFromNib方法中。使此类成为 IB 中滑块实例的类。
  3. 将滑块从 NIB 中完全移除,并按照您在方法中所做的那样以编程方式创建它。然后将其作为子视图添加到您的主视图。
于 2010-02-20T15:25:20.703 回答