2013-12-20 40 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); 
相關問題