将UIButton添加到子视图中

3
当用户点击屏幕时,应该出现一个带有按钮的弹出窗口。但我不知道为什么在弹出窗口中没有显示按钮。这是因为它是子视图中的子视图所以出了问题吗?
-(void) popUpWithX:(int)x andY:(int)y {
    CGRect popUpRect = CGRectMake(x, y, 125, 75);
    popUp = [[UIView alloc] initWithFrame:popUpRect];
    popUp.backgroundColor = [UIColor whiteColor];
    popUp.layer.cornerRadius = 7.5f;
    popUp.layer.masksToBounds = YES;

    [self.view addSubview:popUp];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [button setTitle:@"Click me!" forState:UIControlStateNormal];
    [popUp addSubview:button];
}

编辑:

UIButton的坐标可能是错误的吗?我不确定坐标系是来自主视图还是弹出子视图。


坐标来自弹出窗口,而不是主视图。这很可能不是问题所在。 - Chris Loonam
2个回答

2
如果按钮将成为一个子视图的子视图,则需要在将包含该按钮的视图添加到主视图之前添加按钮...即您添加按钮太晚了。
//Move this line to the end of the block
[self.view addSubview:popUp];//call this after you add your subViews to popUp

我测试了一下,但它并没有解决问题。 - Chris Loonam

2

按钮已经存在,但由于maskToBounds设置为YES而不可见。为了测试目的,将其设置为NO。然后修正按钮的x、y坐标。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接