2013-07-08 188 views
3

当用户在屏幕上点击时,弹出窗口应该会显示一个按钮。 但我不知道为什么按钮不显示在PopUp中。是否有问题,因为它是子视图中的子视图?将UIButton添加到子视图

-(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的坐标是错误的?我不确定坐标系是来自主视图还是来自弹出子视图。

+0

坐标来自弹出窗口,而不是主视图。这很可能不是问题。 –

回答

2

按钮存在,但由于maskToBounds设置为YES而不可见。尝试它只是为了测试目的设置为NO。然后修复按钮的x,y坐标。

+0

是的,现在它工作正常。感谢那! – Sebastian

2

如果该按钮将成为子视图的子视图,那么在添加包含它的视图到主视图之前,您需要添加该按钮。你添加按钮的时间太晚了。

//Move this line to the end of the block 
[self.view addSubview:popUp];//call this after you add your subViews to popUp 
+0

我测试了这个,并没有解决问题。 –

+0

没有为我工作... – Sebastian

相关问题