How do I make a custom UISegmentedControl
?
I have 2 images, 1 that should be displayed when the segment is active and the other if the segment is inacti
I wrote something that works as @rpetrich was explaining without placing in a array and in my opinion is the easiest solution to this. Hope someone finds this useful
.h
IBOutlet UIButton *index0;
IBOutlet UIButton *index1;
IBOutlet UIButton *index2;
IBOutlet UIImageView *segMentControl;
-(IBAction)segmentSwitch:(UIButton *) buttonIndexPressed;
.m
-(IBAction)segmentSwitch:(UIButton *) buttonIndexPressed
{
if (buttonIpressed == index0)
{
[segmentControl setImage:[UIImage imageNamed:@"Seg1Sel.png"]];
NSLog(@"index 0 pushed");
index0.enabled = NO;
index1.enabled = YES;
index2.enabled = YES;
}
else if (buttonIpressed == index1)
{
[segmentControl setImage:[UIImage imageNamed:@"Seg2Sel.png"]];
NSLog(@"index 1 pushed");
index0.enabled = YES;
index1.enabled = NO;
index2.enabled = YES;
}
else if (buttonIpressed == index2)
{
[segmentControl setImage:[UIImage imageNamed:@"Seg3Sel.png"]];
NSLog(@"index 2 pushed");
index0.enabled = YES;
index1.enabled = YES;
index2.enabled = NO;
}
}
Try HMSegmentedControl, it allows images and other settings too. Available at https://github.com/HeshamMegid/HMSegmentedControl
Swift 4
This is how I managed it. Make a segmented control and insert segments using the images. Be sure to set one image as selected as this will be the initial selected index. Also set the backgroundColor and tintColor to clear so that segmented control will only show your image.
var newSegmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl()
segmentedControl.insertSegment(with: UIImage(named: "x_Selected.jpg"), at: 0, animated: true)
segmentedControl.insertSegment(with: UIImage(named: "y_Normal.jpg"), at: 1, animated: true)
segmentedControl.addTarget(self, action: #selector(segmentedControlChanged), for: .valueChanged)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.backgroundColor = .clear
segmentedControl.tintColor = .clear
}()
Function called when the value of segmented control changes.
@objc func segmentedControlChanged(sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
sender.setImage(UIImage(named: "x_Selected.jpg"), forSegmentAt: sender.selectedSegmentIndex)
sender.setImage(UIImage(named: "y_Normal.jpg"), forSegmentAt: 1)
} else if sender.selectedSegmentIndex == 1 {
sender.setImage(UIImage(named: "x_Normal.jpg"), forSegmentAt: 0)
sender.setImage(UIImage(named: "y_Selected.jpg"), forSegmentAt: sender.selectedSegmentIndex)
}
}
Hope you find this useful :)
You can use the methods which are described in the iOS developer libary:
http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
Scroll down to the "Customizing Appearance" section. There are methods to set background images for several button states, button divider images, etc.
These methods are only available in iOS5 and later.
@property tintColor
– backgroundImageForState:barMetrics:
– setBackgroundImage:forState:barMetrics:
– contentPositionAdjustmentForSegmentType:barMetrics:
– setContentPositionAdjustment:forSegmentType:barMetrics:
– dividerImageForLeftSegmentState:rightSegmentState:barMetrics:
– setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics:
– titleTextAttributesForState:
– setTitleTextAttributes:forState: