如何在代码中动态创建UISlider?

时间:2010-06-20 12:48:21

标签: iphone cocoa-touch xcode

我想知道如何在运行时创建UISlider。

2 个答案:

答案 0 :(得分:5)

您可以像其他视图一样添加滑块:

UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(50, 50, 100, 40)] autorelease];
[slider addTarget:self action:@selector(controlValueChanged:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 100.00;
slider.maximumValue = 1000.00;
slider.continuous = YES;
[self.view addSubview:slider];

使用适合您需要的框架,并添加到相应的视图(我在此示例中使用了self.view)。您可以根据需要随意配置。 ; - )

答案 1 :(得分:1)

CGRect frame = CGRectMake(50, 170, 205, kSliderHeight);
customSlider = [[UISlider alloc] initWithFrame:frame];
[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
customSlider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:@"yellowslide.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:@"orangeslide.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];