Is it possible to make one segment of a segmented control invisible?
-
If you want only one segment then why to use segment control, you can directly use a button.. – Ajeet Pratap Maurya Nov 21 '11 at 09:11
-
I am using a segmented control in the view and there happen to be another button in the same view that looks exactly like a segment of the segmented control. So I was trying to do it with a single segmented control. If I could make a segment invisible, I get the view exactly as I want it to be. – Xavi Valero Nov 21 '11 at 09:14
-
@JohnValiaveettil you can easily customize a UIButton to anything you want. and there is no way to remove a segment from the segment control. The minimum segment will be 2 only. you cannot reduce further. – Ajeet Pratap Maurya Nov 21 '11 at 09:18
-
Thats what I should be doing. Thanks everybody. – Xavi Valero Nov 21 '11 at 11:17
4 Answers
You can't hide it but you can make its width very very small which will make it invisible for the user. It has to be > 0 because 0 = automatic width.
[yourSegmentedControl setWidth:0.1 forSegmentAtIndex:1];
To be in the safe side, also disable it to reduce the chance of selection to zero.
[mapTypeSC setEnabled:NO forSegmentAtIndex:1];

- 21,461
- 5
- 90
- 86
Though it seems there is no way to hide a segment in a segment control, you could remove a segment from the segment control using removeSegmentAtIndex:animated:
method. You need either insertSegmentWithImage:atIndex:animated:
or
insertSegmentWithTitle:atIndex:animated:
method to insert the segment again.
Instead of hiding/showing a segment you could consider enabling/disabling it using setEnabled:forSegmentAtIndex:
method.

- 51,274
- 23
- 147
- 178
Yes ,try this it's working for me ,It's only one line of code ,
Objective C
[self.segmentControl removeSegmentAtIndex:0 animated:NO];
Swift
segmentControl.removeSegment(at: 0, animated: false)
The code remove 0 index segment and show only one segment invisible.
Hope this helps.

- 5,941
- 2
- 44
- 55
None of the results above worked for me. The one reducing the width did not work because I had set
control.segmentDistribution = .fillEqually
What worked for me was resetting the segment control's count. Here an example when the data source for the segments is ["Option 1", "Option 2", "Option 3"]
. Imagine you want to remove "Option 2":
var segmentDataSource = ["Option 1", "Option 2", "Option 3"]
segmentDataSource.remove(at: 1)
let oldSelectedSegment = control.selectedSegment
control.segmentCount = segmentDataSource.count
control.selectedSegment = max(0, oldSelectedSegment - 1)
for (index, option) in segmentDataSource.enumerated() {
control.setLabel(option, forSegment: index)
}

- 2,080
- 2
- 18
- 38