iOS:单选按钮以编程方式不使用任何图像
iOS : Radio Button Programatically Without using any Image
我一直致力于创建自定义 ui 控件,想知道如何以编程方式将单选按钮添加到 UIView。
我只找到一种解决方案,但它是针对 mac osx 应用程序控制的。
required 结果的图像如下所示。
限制
不想用图片。
谢谢。
单选按钮 - 如下设置拐角半径和棋盘颜色。在 ViewController.h
文件
中取三个按钮
@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;
并在 ViewDidLoad
方法中。
- (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];
}
点击按钮动作。
- (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];
}
在您的 StoryBoard 按钮视图中,将按钮的高度和宽度设置为以下 (20,20)
。取标题名称点(.)及其字体大小System 44.0
并设置边缘如下图。
您的单选按钮是:
我一直致力于创建自定义 ui 控件,想知道如何以编程方式将单选按钮添加到 UIView。
我只找到一种解决方案,但它是针对 mac osx 应用程序控制的。
required 结果的图像如下所示。
限制 不想用图片。
谢谢。
单选按钮 - 如下设置拐角半径和棋盘颜色。在 ViewController.h
文件
@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;
并在 ViewDidLoad
方法中。
- (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];
}
点击按钮动作。
- (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];
}
在您的 StoryBoard 按钮视图中,将按钮的高度和宽度设置为以下 (20,20)
。取标题名称点(.)及其字体大小System 44.0
并设置边缘如下图。
您的单选按钮是: