-4

我有以下代码(刚开始学习ios):

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, 300, 600);
button.frame = frame;   

如何让它显示在我的视图中?并添加标题?

4

3 回答 3

2

ViewDidLoad试试这个 在方法中写下这段代码。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
于 2013-08-16T07:10:36.323 回答
0

使用addSubview添加到视图和使用属性title设置标题

[button setTitle:@"Click me" forState:UIControlStateNormal];
[self.view addSubview:button];
于 2013-08-16T07:10:56.953 回答
0
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, 300, 600);
button.frame = frame; 

此代码是正确的,只需addSubview在您的代码中使用

像这样

[self.view addSubview:button];

并尝试使用此标题

[button setTitle:@"Btn" forState:UIControlStateNormal];
于 2013-08-16T07:13:06.957 回答