Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:55:06+00:00 2026-05-27T00:55:06+00:00

I’m trying to create a UISlider that lets you choose from an array of

  • 0

I’m trying to create a UISlider that lets you choose from an array of numbers. Each slider position should be equidistant and the slider should snap to each position, rather than smoothly slide between them. (This is the behavior of the slider in Settings > General > Text Size, which was introduced in iOS 7.)

iOS 7 Text Size Slider

The numbers I want to choose from are: -3, 0, 2, 4, 7, 10, and 12.

(I’m very new to Objective-C, so a complete code example would be much more helpful than a code snippet. =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:55:07+00:00Added an answer on May 27, 2026 at 12:55 am

    Some of the other answers work, but this will give you the same fixed space between every position in your slider. In this example you treat the slider positions as indexes to an array which contains the actual numeric values you are interested in.

    @interface MyViewController : UIViewController {
        UISlider *slider;
        NSArray *numbers;
    }
    @end
    
    @implementation MyViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        slider = [[UISlider alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:slider];
    
        // These number values represent each slider position
        numbers = @[@(-3), @(0), @(2), @(4), @(7), @(10), @(12)];
        // slider values go from 0 to the number of values in your numbers array
        NSInteger numberOfSteps = ((float)[numbers count] - 1);
        slider.maximumValue = numberOfSteps;
        slider.minimumValue = 0;
    
        // As the slider moves it will continously call the -valueChanged: 
        slider.continuous = YES; // NO makes it call only once you let go
        [slider addTarget:self
                   action:@selector(valueChanged:)
         forControlEvents:UIControlEventValueChanged];
    }
    - (void)valueChanged:(UISlider *)sender {
        // round the slider position to the nearest index of the numbers array
        NSUInteger index = (NSUInteger)(slider.value + 0.5);
        [slider setValue:index animated:NO];
        NSNumber *number = numbers[index]; // <-- This numeric value you want
        NSLog(@"sliderIndex: %i", (int)index);
        NSLog(@"number: %@", number);
    }
    

    Edit: Here’s a version in Swift 4 that subclasses UISlider with callbacks.

    class MySliderStepper: UISlider {
        private let values: [Float]
        private var lastIndex: Int? = nil
        let callback: (Float) -> Void
        
        init(frame: CGRect, values: [Float], callback: @escaping (_ newValue: Float) -> Void) {
            self.values = values
            self.callback = callback
            super.init(frame: frame)
            self.addTarget(self, action: #selector(handleValueChange(sender:)), for: .valueChanged)
            
            let steps = values.count - 1
            self.minimumValue = 0
            self.maximumValue = Float(steps)
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        @objc func handleValueChange(sender: UISlider) {
            let newIndex = Int(sender.value + 0.5) // round up to next index
            self.setValue(Float(newIndex), animated: false) // snap to increments
            let didChange = lastIndex == nil || newIndex != lastIndex!
            if didChange {
                lastIndex = newIndex
                let actualValue = self.values[newIndex]
                self.callback(actualValue)
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.