我试图将UISlider放在UIView内部,但是它一直排在左侧?这是代码。
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (UIView *)manufactorSliderWithLabel:(NSString *)sliderText
url:(NSString *)urlPath
callback:(NSString *)method
message:(NSString *)message
status:(NSString *)status {
// Load the track background
UIImage *trackImage = [UIImage imageNamed:@"sliderTrack.png"];
UIImageView *sliderBackground = [[UIImageView alloc] initWithImage:trackImage];
// Create the superview same size as track backround, and add the background image to it
UIView *view = [[UIView alloc] initWithFrame:sliderBackground.frame];
[view addSubview:sliderBackground];
view.backgroundColor = [UIColor redColor];
// Add the slider with correct geometry centered over the track
UISlider* slider = [[UISlider alloc] initWithFrame:sliderBackground.frame];
CGRect sliderFrame = slider.frame;
sliderFrame.size.width -= 3; //each "edge" of the track is 23 pixels wide
slider.frame = sliderFrame;
slider.center = sliderBackground.center;
slider.backgroundColor = [UIColor clearColor];
[slider setMinimumTrackImage:[UIImage imageNamed:@"sliderMaxMin-02.png"] forState:UIControlStateNormal];
[slider setMaximumTrackImage:[UIImage imageNamed:@"sliderMaxMin-02.png"] forState:UIControlStateNormal];
UIImage *thumbImage = [UIImage imageNamed:@"sliderThumbGreen_normal.png"];
[slider setThumbImage:thumbImage forState:UIControlStateNormal];
slider.minimumValue = 0.0;
slider.maximumValue = 1.0;
slider.continuous = YES;
slider.value = 0.0;
slider.url = urlPath;
slider.message = message;
slider.socketStatus = status;
slider.callbackMethod = method;
// Set the slider action methods
[slider addTarget:self
action:@selector(sliderUp:)
forControlEvents:UIControlEventTouchUpInside];
[slider addTarget:self
action:@selector(sliderDown:)
forControlEvents:UIControlEventTouchDown];
[slider addTarget:self
action:@selector(sliderChanged:)
forControlEvents:UIControlEventValueChanged];
// Create the label with the actual size required by the text
// If you change the text, font, or font size by using the "label" property,
// you may need to recalculate the label's frame.
NSString *labelText = NSLocalizedString(sliderText, @"SlideToCancel label");
UIFont *labelFont = [UIFont systemFontOfSize:12];
CGSize labelSize = [labelText sizeWithFont:labelFont];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, labelSize.width, labelSize.height)];
// Center the label over the slidable portion of the track
CGFloat labelHorizontalCenter = slider.center.x + (thumbImage.size.width / 2);
label.center = CGPointMake(labelHorizontalCenter, slider.center.y);
// Set other label attributes and add it to the view
label.textColor = [UIColor blackColor];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = labelFont;
label.text = labelText;
[view addSubview:label];
slider.backgroundColor = [UIColor greenColor];
[view addSubview:slider];
return view;
}
答案 0 :(得分:0)
如果你想将滑块对应于sliderBackground,那么首先设置sliderBackground的框架,因为在你的代码中我可以看到你没有设置sliderBackground的框架,然后执行
slider.center = sliderBackground.center;