iOS:单选按钮以编程方式不使用任何图像

时间:2015-03-16 11:13:27

标签: ios objective-c radio-button

我一直致力于创建自定义ui控件,并想知道如何以编程方式向UIView添加单选按钮。

我只发现了一个解决方案,但它适用于mac osx应用程序控制。 所需结果的图像为。 enter image description here

限制 不想使用图片。

感谢。

1 个答案:

答案 0 :(得分:4)

单选按钮 - 设置角半径和板颜色如下。在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)。取标题名称Dot(。)及其字体大小System 44.0,并将Edge设置为图像下方。

enter image description here

您的单选按钮是:

enter image description here

相关问题