2013-12-20 15 views
0

我有下面的代码,添加一个UIView,然后添加一个UIImageView和UILabel,UIView显示正确,但没有任何我放在视图显示。我到处寻找,但我找不到答案。 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]; 
+1

您的标签帧与帧的ImageView走出去的边界的UIView。 –

回答

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

当你设置视图的框架,要记住,产地涉及到它被添加到了上海华盈的坐标系中是非常重要的。

所以,当您在标签的框架设置为:

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

的X = 20,Y = 180结束了为原点内部的actionView原点。现在这是你的问题,因为你的actionView只有165px高,并且你正在告诉actionText子视图,它的y原点是180,这远远超出了actionView的底部。

您actionText,你应该如下分配它:

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

,并为您的BlockView用来,是这样的:

blockView.frame = CGRectMake(89,93, 40, 40); 
相关问题