0

I am working with segemntController,

I am setting different different images for pressed and normal state of segments, below is my code

 (void)viewdidLoad
    {

NSArray *imageArray = [NSArray arrayWithObjects: [UIImage imageNamed:@"image1"],[UIImage imageNamed:@"image2"],[UIImage imageNamed:@"image3"],nil];

    m_segmentController = [[UISegmentedControl alloc]initWithItems:imageArray];



    m_segmentController.frame = CGRectMake(50, 10, 212, 30);
        m_segmentController.segmentedControlStyle = UISegmentedControlStyleBar;
            m_segmentController.selectedSegmentIndex = 0;

       [m_segmentController setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [m_segmentController addTarget:self action:@selector(segmentSelected:)forControlEvents:UIControlEventValueChanged];

     [self.navigationController.navigationBar addSubview:m_segmentController];
    }




    - (IBAction)segmentSelected:(id)sender
    {
        UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
        NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

        if(selectedSegment == 0)
        {
             [m_segmentController setImage:[UIImage imageNamed:@"image1.png"] forSegmentAtIndex:0];
         }
         else  
          {
             [m_segmentController setImage:[UIImage imageNamed:@"image2png"] forSegmentAtIndex:1];        
          }  


        }

So when I start my app, the segmentController, looks this way

enter image description here

but when I select any segment the selected image is not filling the entire segment and looks like this

enter image description here

How to solve this issue, where I am going wrong

Regards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121

1 Answers1

0

you can use idevrecipes.com/2010/12/11/custom-segmented-controls/.

This link has some best customize UISegmentControl

EDIT

for your problem you have to use custom segment control. currently you can set image to your segment not background image.

If you want to use default UISegment control you have to make exact size image with the size of your particular segment. then you can adjust image by setting ContentOffSets of a segment.

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
  • hi, thanks for your suggestion, but their is no need to use any custom control for it. ios 5 has provided many options for it. – Ranjit Jun 26 '13 at 14:27