我以编程方式生成自定义视图控制器的视图,包括添加几个按钮。我连续有三个按钮,都沿着这一个按钮创建:
self.futureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, buttonOffset, _width/3, _height*.1)];
self.futureButton.backgroundColor = [UIColor pinkColor];
self.futureButton.tag = 1;
[self.futureButton setTitle:@"Upcoming" forState: UIControlStateNormal];
[self.futureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[self setColor:[UIColor lightPinkColor] forState:UIControlStateSelected forButton:self.futureButton];
[self.futureButton addTarget:self action:@selector(changAppt:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.futureButton];
我也在最后这样做,将相同的属性应用于所有这些按钮:
for(UIButton *myButton in self.view.subviews){
if([myButton isKindOfClass:[UIButton class]]){
[[myButton layer] setBorderWidth:4.0f];
[[myButton layer] setBorderColor:[UIColor whiteColor].CGColor];
[myButton.layer setCornerRadius:12.0f];<----problem
}
}
除了最后一个命令setCornerRadius之外,一切都运行良好。如果我评论它,我有我想要的 - 所有按钮排成一行,白色边框消失在背景白色。但是,如果我使用最后一个命令setCornerRadius,我会得到以下内容(见图)
同样奇怪的效果是选择了哪个按钮。我是否必须对选定的状态效果和CornerRadius做些什么?如果是这样,是什么?