以编程方式添加UIButton将不会显示

时间:2014-07-30 12:37:59

标签: objective-c uiscrollview uibutton ios8

我目前正在为iOS 8发布的应用程序商店准备iOS 8 application,我显然不会我无法显示太多代码,因为我保持项目非常安静但我正在创建一个带有滚动视图和许多对象的视图控制器来为用户创建一个配置文件,但是我正在尝试向所有用户添加一个按钮以将项目添加到他们的配置文件中,并且该按钮不会被添加滚动视图...是否存在UIButtonUIScrollView s的未写入规则?

下面是我用来创建按钮的代码

UIButton *addProject = [[UIButton alloc] initWithFrame:CGRectMake(210, 285, 100, 18)];
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];

我是否需要以不同的方式将其添加到滚动视图?

编辑1: _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 699);

编辑2:此外,滚动视图在故事板中创建,框架为0, 64, 320, 455,但上面的内容大小有更高的高度

2 个答案:

答案 0 :(得分:9)

试试这个;

UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(210, 285, 100, 18);
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];

尝试添加此行;

addProject.backgroundColor = [UIColor redColor];

由于相同的背景颜色,它可能不会出现。

答案 1 :(得分:2)

UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(210, 285, 100, 18);
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];
_scrollView.backgroundColor = [UIColor clearColor];