Search code examples
iosxcodeuiscrollviewuislider

iOS custom scrollbar for UIScrollView


iOS scrollbars for a UIScrollView usually hide after a brief delay, but I want to the scrollbars in my app to show all the time like in this picture: Scrollbars visible all the time

Do you have any Idea how to implement this feature? Can I use a custom UISlider for this? If you can provide answers, please do so in simple English because it's not my native language. Thank you!


Solution

  • There is no direct way to show scrollbar permanent.

    You can do this by UISlider and UIImageView.

    1) Add UImage View for Vertical line.

    UIImageView *scrollLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scroll_1-1.png"]];
    scrollLineImageView.frame = CGRectMake(970,160,2,254);
    [self.view addSubview:scrollLineImageView];
    

    2) Add UISlider and transform to UISlider vertical .UISlider height should be your scroll view height.

    CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * 0.5);
    self.scrollViewSlider.transform = trans;
    self.scrollViewSlider.frame = CGRectMake(972.5,189,0, self.textScrollView.frame.size.height);
    Set Slider minimum value to 0 and maximum value to scrollView content height.
    self.scrollViewSlider.minimumValue = 0.0;
    self.scrollViewSlider.maximumValue = self.textScrollView.contentSize.height;
    

    3) Set UISlider Thumb Image

    [self.scrollViewSlider setThumbImage:[UIImage imageNamed:@"scroll_circle.png"] forState:UIControlStateNormal];
    

    4) Clear Minimum and Maximum track Image.

    UIImage *clearImage = [[UIImage alloc] init];
    [self.scrollViewSlider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
    [self.scrollViewSlider setMaximumTrackImage:clearImage forState:UIControlStateNormal];
    

    5) ScrollView Delegate Function to scroll slider

    self.scrollViewSlider.value=scrollView.contentOffset.y;
    

    I hope this helps you to show Scroll Bar Permanent.