简体   繁体   中英

Setting the maximum value of a UISlider causing strange behaviour

I am having a really weird issue trying to use a UISlider, when I change the maximumValue of the slider to > 1.0, I get this weird duplicate slider appearing on my view. Please help :)

Heres what it looks like when slider.maximumValue = 10.0; http://imagizer.imageshack.us/v2/800x600q90/827/kf12.png

Heres what it looks like when slider.maximumValue = 1.0; http://imagizer.imageshack.us/v2/800x600q90/197/bcvf.png

Is there some behaviour of a UISlider I am missing? Here is my code for configuring the slider...

-(void)layoutSubviews
{
    [super layoutSubviews];

    CGSize viewSize = self.contentSubview.bounds.size;

    self.timeSlider.frame = CGRectMake(viewSize.width / 2 - 80,
                                       viewSize.height / 2 - 12,
                                       viewSize.width / 2,
                                       24);

    -(void)configureSubviews
    {
        [super configureSubviews];

        self.timeSlider = [[UISlider alloc] initWithFrame:CGRectZero];
        self.timeSlider.continuous = YES;
        self.timeSlider.minimumValue = 0;
        self.timeSlider.maximumValue = 1.0f;
        [self.timeSlider addTarget:self action:@selector(timeValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self.contentSubview addSubview:self.timeSlider];

    }

    -(void)timeValueChanged:(UISlider *)slider
    {
        self.timeValueLabel.text = [NSString stringWithFormat:@"%d min", (int)floorf(slider.value)];
        [self layoutSubviews];
    }

layoutSubviews Lays out subviews.

- (void)layoutSubviews

Discussion

The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

You should not call this method directly . If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.

Referance by apple doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM