iOS 8.0升级后UILabel无法显示

时间:2014-09-30 01:04:32

标签: ios objective-c xcode

今天早上刚刚更新到iOS 8.0。我有一段代码创建了一些标签,并在一些图像旁边的scree上显示它们。在更新之前它工作正常。现在标签和图像没有显示。有人可以帮助,因为我已经尝试了我能想到的一切,没有解决我的问题,也找不到其他有类似问题的人。代码见下文。

UIView *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, kCategoryLabelHieght)];
labelView.backgroundColor = [self colorWithHexString:@"2D2D2D"];

UILabel *categoryTitle = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 200, kCategoryLabelHieght)];
categoryTitle.textAlignment = NSTextAlignmentLeft;
categoryTitle.text = text;
categoryTitle.font = [UIFont fontWithName:@"HelveticaNeue" size:14];

codeAppDelegate *appDelegate = (codeAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *fetchedCategory = [appDelegate getCategoryByName:text];

UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 2, 20, 20)];

if(fetchedCategory.count)
{
    for (int t=0; t<fetchedCategory.count; t++) {
        Dealcategory_List *catList = [fetchedCategory objectAtIndex:t];
        NSString *titleImage = [self URLEncodeString:[NSString stringWithFormat:@"%@",catList.image]];
        NSURL *url = [NSURL URLWithString:titleImage];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *imgTabs = [UIImage imageWithData:data];
        //        [header2 setImage:imgTabs forState:UIControlStateNormal];
        [imgTabs resizableImageWithCapInsets: UIEdgeInsetsMake(5, 10, 2, 0) resizingMode: UIImageResizingModeTile];

        img.image= imgTabs;
    }
}
else
{
    NSArray *fetchedNewsCategory = [appDelegate getNewsCategoryByName:text];

    if(fetchedNewsCategory.count > 0)
    {
        for (int t=0; t<fetchedNewsCategory.count; t++) {
            News_Categories *catList = [fetchedNewsCategory objectAtIndex:t];
            NSString *titleImage = [self URLEncodeString:[NSString stringWithFormat:@"%@",catList.category_image]];
            NSURL *url = [NSURL URLWithString:titleImage];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *imgTabs = [UIImage imageWithData:data];
            //        [header2 setImage:imgTabs forState:UIControlStateNormal];
            [imgTabs resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 2, 0) resizingMode: UIImageResizingModeTile];

            img.image= imgTabs;
        }
    }
    else
    {
        NSArray *fetchedCompetition = [appDelegate getCompetitionByName:text];

        if(fetchedCompetition.count > 0)
        {
            for (int t=0; t<fetchedCompetition.count; t++) {
                Competitions_List *catList = [fetchedCompetition objectAtIndex:t];
                NSString *titleImage = [self URLEncodeString:[NSString stringWithFormat:@"%@",catList.competition_tab_image]];
                NSURL *url = [NSURL URLWithString:titleImage];
                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage *imgTabs = [UIImage imageWithData:data];
                //        [header2 setImage:imgTabs forState:UIControlStateNormal];
                [imgTabs resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 2, 0) resizingMode: UIImageResizingModeTile];

                img.image= imgTabs;

                break;
            }
        }
    }
}

categoryTitle.textColor = color;
[labelView addSubview:img];
[labelView addSubview:categoryTitle];

[self.contentView addSubview:labelView];

1 个答案:

答案 0 :(得分:1)

UIView *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, kCategoryLabelHieght)];

您正在定义UIView,但正在分配UILabel。 iOS 8对此类错误的容忍度较低。尝试将其更改为

UIView *labelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, kCategoryLabelHieght)];