Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding UIButton to subView

Tags:

ios

iphone

When the User taps on the screen a PopUp should appear with a button in it. But i don't know why the button isn't shown in the PopUp. Is there a problem because it is a subview in a subview ?

-(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];
}

EDIT:

Is it possible that the coordinates of the UIButton are wrong ? I am not sure if the coordinate system is from the main view or from the popUp subview.

like image 440
Sebastian Avatar asked Dec 07 '25 00:12

Sebastian


2 Answers

Button is there, but is not visible because of maskToBounds set to YES. Try it just set to NO just for testing purposes. Then fix your x, y coordinates for button.

like image 196
alexhajdu Avatar answered Dec 08 '25 14:12

alexhajdu


If the button is going to be a subview of a subview, then you need to add the button prior to adding the view containing it to the main view....i.e. you're adding the button too late.

//Move this line to the end of the block
[self.view addSubview:popUp];//call this after you add your subViews to popUp
like image 36
Tommy Devoy Avatar answered Dec 08 '25 14:12

Tommy Devoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!