UIView不允许我添加子视图

时间:2012-12-21 03:44:30

标签: ios xcode uiview uiviewcontroller addsubview

所以我有一个视图,在我的viewcontoller类中,我尝试使用以下代码添加子视图。

    UIButton *test = [[UIButton alloc]initWithFrame:CGRectMake(30, 100, 100, 100)];
    [self.view addSubView:test];

没有任何事情发生,没有错误或任何事情。我知道它被调用,因为它在viewdidload方法中。我有一个单独的类,我的子类uiview可以与它有什么关系吗?

2 个答案:

答案 0 :(得分:1)

UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
test.frame = CGRectMake(30, 100, 100, 100);
[self.view addSubView:test];

答案 1 :(得分:0)

您需要确保添加该按钮的所有必需属性:

UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test addTarget:self action:@selector(selector:) forControlEvents:UIControlEventTouchDown];
[test setTitle:@"Button Title" forState:UIControlStateNormal];
test.frame = CGRectMake(30, 100, 100, 100);
[view addSubview:test];