@Christopher Fairbaim建议使用UIButtonTypeCustom。使用UIButtonTypeRoundedRect将为您提供透明区域中的白色背景。
但是,在Interface Builder中进行接线有其局限性。如果您想从数据集或基于用户输入动态生成UIViews,UIButtons和资产,可以通过编程方式进行操作。
创建的按钮:(注:这也将与其他UIButtonTypes工作,但我用UIButtonTypeCustom这个特定透明度的例子。)
UIButton *btnName = [UIButton buttonWithType:UIButtonTypeCustom];
要更改所需的属性:
btnName.backgroundColor = [UIColor clearColor];
btnName.frame = CGRectMake(100, 100, 72, 37); //(X coord, Y coord, width, height)
[btnName addTarget:myActionClassName action:@selector(myActionName) forControlEvents:UIControlEventTouchUpInside];
要为各种状态添加图像(假设这些都是透明的PNG的):
UIImage *btnNameImageNormal = [UIImage imageNamed:@"btnNameNormal.png"];
[btnName setBackgroundImage:btnMenuImageNormal forState:UIControlStateNormal];
UIImage *btnNameImagePressed = [UIImage imageNamed:@"btnNamePressed.png"];
[btnName setBackgroundImage:btnNameImagePressed forState:UIControlStateHighlighted];
要将按钮添加到图(从的viewController类中):
[self addSubview:btnName];
(赦免任何错别字,因为我复制并从我的一个项目粘贴,并试图推广的命名。)