我正在以编程方式创建 View ,当我添加一个带有标题的按钮时,我看到的只是一些文本和清晰的背景。有没有一种方法可以获得具有圆角、边框和按下状态的默认外观按钮,而无需手动指定每个属性?
如果是后者,是否有某个地方的样式指南以便我可以获得默认外观的按钮?
最佳答案
试试这个:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 30, 30);
button.titleLabel.text = @"A Button";
[self.view addSubview: button];
这是每一行的作用:
- 第一行设置按钮类型为圆角矩形,但也可以是
UIButtonTypeCustom
、UIButtonTypeDetailDisclosure
、UIButtonTypeInfoLight
、UIButtonTypeInfoDark
,UIButtonTypeContactAdd
。 - 第二行设置按钮的框架,即按钮在屏幕上的位置以及按钮的高度和宽度。
- 第三行将按钮的文本设置为“A Button”。
- 第四行也是最后一行将按钮添加到屏幕。
按钮看起来像这样:
关于objective-c - iOS 有没有默认的按钮样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11595192/