Iphone 将UIButton添加到子视图
当用户点击屏幕时,弹出窗口中会出现一个按钮。 但我不知道为什么按钮没有显示在弹出窗口中。是否存在问题,因为它是子视图中的子视图Iphone 将UIButton添加到子视图,iphone,ios,Iphone,Ios,当用户点击屏幕时,弹出窗口中会出现一个按钮。 但我不知道为什么按钮没有显示在弹出窗口中。是否存在问题,因为它是子视图中的子视图 -(void) popUpWithX:(int)x andY:(int)y { CGRect popUpRect = CGRectMake(x, y, 125, 75); popUp = [[UIView alloc] initWithFrame:popUpRect]; popUp.backgroundColor = [UIColor white
-(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的坐标有可能是错误的吗?我不确定坐标系是从主视图还是从弹出子视图 如果按钮将成为子视图的子视图,则需要在将包含该按钮的视图添加到主视图之前添加该按钮……即。你添加按钮太晚了
//Move this line to the end of the block
[self.view addSubview:popUp];//call this after you add your subViews to popUp
按钮存在,但不可见,因为maskToBounds设置为“是”。尝试将其设置为“否”,仅用于测试目的。然后修复按钮的x、y坐标。我测试了这个,但没有解决问题。坐标来自弹出窗口,而不是主视图。这很可能不是问题所在。