利用UIButton可以创建圆角和方形的按钮,这些按钮比较简单实用,经常可以在购买网站看到。 如下图效果
CGRect frame = CGRectMake(300, 100, 40, 40);
UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeSystem];
addBtn.frame = frame;
[addBtn setTitle:@"+" forState: UIControlStateNormal];
[addBtn setTitle:@"+" forState: UIControlStateHighlighted];
[addBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[addBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
addBtn .titleLabel.font = [UIFont systemFontOfSize:18];
addBtn.layer.cornerRadius = addBtn.frame.size.width/2;
addBtn.layer.borderColor = [UIColor redColor].CGColor;
addBtn.layer.borderWidth = 1;
addBtn.center = CGPointMake(self.view.center.x+50,self.view.center.y);
[self.view addSubview:addBtn];
UIButton *subBtn = [UIButton buttonWithType:UIButtonTypeSystem];
subBtn.frame = frame;
[subBtn setTitle:@"-" forState: UIControlStateNormal];
[subBtn setTitle:@"-" forState: UIControlStateHighlighted];
[subBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[subBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
subBtn .titleLabel.font = [UIFont systemFontOfSize:18];
subBtn.layer.cornerRadius = subBtn.frame.size.width/2;
subBtn.layer.borderColor = [UIColor redColor].CGColor;
subBtn.layer.borderWidth = 1;
subBtn.center = CGPointMake(self.view.center.x-50,self.view.center.y);
[self.view addSubview:subBtn];
UIButton *addRBtn = [UIButton buttonWithType:UIButtonTypeSystem];
addRBtn.frame = frame;
[addRBtn setTitle:@"+" forState: UIControlStateNormal];
[addRBtn setTitle:@"+" forState: UIControlStateHighlighted];
[addRBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[addRBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
addRBtn .titleLabel.font = [UIFont systemFontOfSize:18];
addRBtn.layer.cornerRadius = 4;
addRBtn.layer.borderColor = [UIColor redColor].CGColor;
addRBtn.layer.borderWidth = 1;
addRBtn.center = CGPointMake(self.view.center.x+50,self.view.center.y+50);
[self.view addSubview:addRBtn];
UIButton *subRBtn = [UIButton buttonWithType:UIButtonTypeSystem];
subRBtn.frame = frame;
[subRBtn setTitle:@"-" forState: UIControlStateNormal];
[subRBtn setTitle:@"-" forState: UIControlStateHighlighted];
[subRBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[subRBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
subRBtn .titleLabel.font = [UIFont systemFontOfSize:18];
subRBtn.layer.cornerRadius = 4;
subRBtn.layer.borderColor = [UIColor redColor].CGColor;
subRBtn.layer.borderWidth = 1;
subRBtn.center = CGPointMake(self.view.center.x-50,self.view.center.y+50);
[self.view addSubview:subRBtn];
编译后 可以看到效果
做圆角还是圆形取决于需求,而这两个区别仅仅是对Layer 属性的layer.cornerRadius 设置而已。