为什么我的UIView中没有显示任何内容?

时间:2013-12-20 11:39:16

标签: ios iphone objective-c uiview xcode5

我有以下代码添加一个UIView,然后添加一个UIImageView和UILabel,UIView显示正确,但我放在视图中的任何内容都没有显示。我到处都看,但我找不到答案。 UIView是在某些条件下呈现的,当它被点击时它会消失。

这是我的代码。

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

2 个答案:

答案 0 :(得分:2)

更改框架位置,例如..

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

答案 1 :(得分:2)

设置视图的框架时,重要的是要记住原点与添加到它的超视图的坐标系有关。

因此,当您将标签的框架设置为:

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

x = 20的原点,y = 180最终成为actionView的原点。现在这是你的问题,因为你的actionView只有165px高,你告诉actionText subView它的y origin是180,它远远超出了actionView的底部。

对于你的actionText,你应该按如下方式分配它:

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

和你的blockView,如:

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