Danial Hussain
Danial Hussain

Reputation: 47

iOS : Radio Button Programatically Without using any Image

I have been working on creating custom ui controls and want to know how to add radio button to UIView programatically.

I only found one solution but it is for mac osx application control. Image of required result is given as. enter image description here

LIMITATION Not want to use image.

Thanks.

Upvotes: 1

Views: 3713

Answers (1)

Kirit Modi
Kirit Modi

Reputation: 23407

Radio button - Set corner radius and board color as below. Take three button in ViewController.h file

@property(nonatomic,retain) IBOutlet UIButton *btn1;
@property(nonatomic,retain) IBOutlet UIButton *btn2;
@property(nonatomic,retain) IBOutlet UIButton *btn3;

- (IBAction)ClickBtn1:(id)sender;
- (IBAction)ClickBtn2:(id)sender;
- (IBAction)ClickBtn3:(id)sender;

And in ViewDidLoad method.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.btn1.layer.cornerRadius = 10;
    self.btn1.layer.borderColor = [[UIColor blackColor] CGColor];

    self.btn2.layer.cornerRadius = 10;
    self.btn2.layer.borderColor = [[UIColor blackColor] CGColor];

    self.btn3.layer.cornerRadius = 10;
    self.btn3.layer.borderColor = [[UIColor blackColor] CGColor];

    // default

    [self.btn1 setTitle:@"." forState:UIControlStateNormal];
    [self.btn2 setTitle:@"" forState:UIControlStateNormal];
    [self.btn3 setTitle:@"" forState:UIControlStateNormal];

}

Button Action on click.

- (IBAction)ClickBtn1:(id)sender
{
    [self.btn1 setTitle:@"." forState:UIControlStateNormal];
    [self.btn2 setTitle:@"" forState:UIControlStateNormal];
    [self.btn3 setTitle:@"" forState:UIControlStateNormal];
}
- (IBAction)ClickBtn2:(id)sender
{
    [self.btn2 setTitle:@"." forState:UIControlStateNormal];
    [self.btn1 setTitle:@"" forState:UIControlStateNormal];
    [self.btn3 setTitle:@"" forState:UIControlStateNormal];
}
- (IBAction)ClickBtn3:(id)sender
{
    [self.btn3 setTitle:@"." forState:UIControlStateNormal];
    [self.btn1 setTitle:@"" forState:UIControlStateNormal];
    [self.btn2 setTitle:@"" forState:UIControlStateNormal];
}

in Your StoryBoard button View set as below height and Width (20,20) of the button. Take title name Dot (.) and its font size System 44.0and set Edge as below image.

enter image description here

Your Radio button is :

enter image description here

Upvotes: 5

Related Questions