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

  • Home
  • SEARCH
  • 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 3443002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:44:18+00:00 2026-05-18T08:44:18+00:00

I am creating a UISegmentedControl replacement so that it works with your own custom

  • 0

I am creating a UISegmentedControl replacement so that it works with your own custom images. Because the separators need different colors on both sides I decided to give the middle item the two borders.

Now to display the UISegmentedControl replacement, I calculate the available width for one item (frame.size.width / numberOfItems). Then I create a UIButton with a custom background image (the stretchable middle segment image). The next thing is to position everything. Because the 1px separators need to be visible when you select an item, I give every item a 1px larger frame than it actually should have. So the next item overlaps 1px to the left.

segmentRect = CGRectMake(indexOfObject * (self.frame.size.width / numberOfSegments), 0, (self.frame.size.width / numberOfSegments) + 1, self.frame.size.height);

Using this result I get a nearly perfect custom UISegmentedControl (retina): alt text

Now things look pretty ok, but it all changes when adding more/less segments. This was a 300px wide control by the way, so each segment gets 25px of space. If I change that number to let’s say 13, this shows up: alt text

Notice the slightly different border between the ‘3’ and ‘4’. (Easier to spot on non-retina actually because of pixel doubling) I think this is caused by the not so nice amount of space each segment gets. (300 / 13 =
23,0769) One should think that the stretchable image would accommodate for this, no? The separators are exactly 1px in width and I change the frame by 1px, so the two separators should be placed exactly on top of each other, which is definitely not the case here.

Does anyone have an explanation why this happens and more important a way to fix this?

  • 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-18T08:44:18+00:00Added an answer on May 18, 2026 at 8:44 am

    All coordinates are floats (CGFloat’s, actually) by default, so if you’re handing the system non-integer coordinates it will try its best to draw between pixels, using anti-aliasing. The weird 3/4 border is probably due to this.

    One solution is to give all your segments integer-sized widths. This may sound a bit nasty, since they might not all be the same width in order to fill up the space (i.e. if the overall width is not divisible by the number of segments), but code like the following shows how this can be done without too much trouble:

    int totalWidth = self.frame.size.width;
    float dx = (float)totalWidth / numSegments;
    float lastX = 0.0;
    for (int i = 0; i < numSegments; ++i) {
      int thisWidth = round(lastX + dx) - round(lastX);
      CGRect segmentFrame = CGRectMake(round(lastX), 0, thisWidth, height);
      /* do what you will with segmentFrame */
      lastX += dx;
    }
    

    I personally like this technique because it automatically evenly distributes the widths that are slightly larger than the others (all the widths will be within 1 pixel of each other).

    This code is not including your 1-pixel overlaps, but that is easy to add.

    Another benefit is that if your UILabel’s or UIImageView’s also have integer coordinates (in the coordinate system of the entire screen), they tend to look more crisp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating the permutations of a list or set is simple enough. I need to
Creating a patch utility that will update my current website with my patch. when
Creating JQGRID object i put hiddengrid:true. Now, to expand grid i need to press
Creating a voting system just like stackoverflow. Questions like this have been asking several
Creating a layout in Java as the number of TableLayouts required is not known
Creating my tables from my models.py . I donno how to do 2 things
Creating a zip file using Send To -> Compressed folder excludes .hg folder on
Creating a click event for an input is easy $('input').click(function () { alert('adsf'); });
Creating an Activity with a Service and setting up the bindService. I thought i

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.