Jason Wragg
Jason Wragg

Reputation: 589

Why is nothing displaying in my UIView?

I have following code that adds a UIView and then adds a UIImageView and UILabel, the UIView display correctly but nothing i have put in side the view is showing. I have looked everywhere but i cannot find an answer. The UIView is presented under certain conditions and when it is tapped it dissapears.

Here is my code.

UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
        UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [actionView addGestureRecognizer:singleFingerTap];
        actionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
        actionView.layer.cornerRadius = 5;
        actionView.layer.masksToBounds = YES;
        [self.view addSubview:actionView];
        UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
        actionText.text = @"Hello";
        actionText.textColor = [UIColor redColor];
        actionText.textAlignment = NSTextAlignmentCenter;
        [actionText setTextColor:[UIColor redColor]];
        [actionText setBackgroundColor:[UIColor clearColor]];
        [actionText setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
        actionText.Frame = CGRectMake(20, 180, 280, 165);
         [actionView addSubview:actionText];
        UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"trash-icon.png"]];
        UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
        blockView.frame = CGRectMake(109, 273, 40, 40);
      [actionView addSubview:blockView];

Upvotes: 0

Views: 113

Answers (2)

tjboneman
tjboneman

Reputation: 668

When you set the frame of a view, it is important to remember that the origin relates to the coordinate system of the superview it is added to.

So when you set the frame of your label to:

UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];

the origin of x = 20, y = 180 end up being the origin WITHIN the actionView. Now this is your problem because your actionView is only 165px tall, and you are telling the actionText subView that it's y origin is 180, which is well outside the bottom of the actionView.

for your actionText, you should allocate it as follows:

UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 165)];

and for your blockView, something like:

blockView.frame = CGRectMake(89,93, 40, 40);

Upvotes: 2

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

Change your frame positions like..

    UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
    actionText.text = @"Hello";
    actionText.textColor = [UIColor redColor];
    actionText.textAlignment = NSTextAlignmentCenter;
    [actionText setTextColor:[UIColor redColor]];
    [actionText setBackgroundColor:[UIColor clearColor]];
    [actionText setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
    [actionView addSubview:actionText];

    UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"trash-icon.png"]];
    UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
    blockView.frame = CGRectMake(109,70, 40, 40);
    [actionView addSubview:blockView]; 

Upvotes: 2

Related Questions