使用图像是一种方法,但我想您已经在您的问题中明确表示您不希望使用图像。这是我以编程方式创建和着色的按钮。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:@"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
您可以随意更改[UIColor ColorWithRed: wo,甚至可以使用[UIColor redColor] 或任何其他您想做的事情。
这是一个屏幕截图:
无法为圆形按钮添加背景颜色,请将按钮类型更改为自定义,然后使用石英芯设置圆形按钮。因此,将其添加到您的项目中并将其导入到 .m 中,它应该可以工作。希望这对你有用。
要将按钮更改为类似于您显示的图像的内容,请将框架更改为:
btn.frame = CGRectMake(50.0, 100.0, 200.0, 50.0);
并将半径更改为:
btn.layer.cornerRadius = 7;
应该这样做。