所以我有一个视图,在我的 viewcontoller 类中,我尝试使用以下代码添加一个子视图。
    UIButton *test = [[UIButton alloc]initWithFrame:CGRectMake(30, 100, 100, 100)];
    [self.view addSubView:test];
什么都没有发生,没有错误或任何东西。我知道它被调用是因为它在 viewdidload 方法中。我有一个单独的类,我是 uiview 的子类,这与它有什么关系吗?
所以我有一个视图,在我的 viewcontoller 类中,我尝试使用以下代码添加一个子视图。
    UIButton *test = [[UIButton alloc]initWithFrame:CGRectMake(30, 100, 100, 100)];
    [self.view addSubView:test];
什么都没有发生,没有错误或任何东西。我知道它被调用是因为它在 viewdidload 方法中。我有一个单独的类,我是 uiview 的子类,这与它有什么关系吗?
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
test.frame = CGRectMake(30, 100, 100, 100);
[self.view addSubView:test];
您需要确保添加该按钮的所有必需属性:
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];