设置 UILabel 背景颜色导致应用程序崩溃 [英] Setting UILabel background color causes app to crash

查看:35
本文介绍了设置 UILabel 背景颜色导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

UIImageView *photo = [[[UIImageView alloc] initWithFrame:CGRectMake(title_bar.titleView.frame.origin.x+130, title_bar.titleView.frame.origin.y-12, 22.0, 22.0)] autorelease];
UIImage *theImage = [UIImage imageNamed:@"question mark icon"];
photo.image = theImage;
UIView *new_view = [[UIView alloc] init];
[new_view addSubview:photo];
UILabel *new_label = [[UILabel alloc] initWithFrame:CGRectMake(title_bar.titleView.frame.origin.x-145, title_bar.titleView.frame.origin.y-12, 268.0, 22.0)];
[new_label setText:@"gregrgtg"];
[new_label setBackgroundColor:[UIColor clearColor]]; //COMMENTING THIS LINE OUT MAKES IT WORK
[new_view addSubview:new_label];
title_bar.titleView = new_view;

当背景颜色设置为clear时,程序SIGABRTs.如果我注释掉该行,它就可以正常工作(除了标签的背景只是白色).另外,如果我只是初始化标签而不是 initWithFrame ,程序不会崩溃,但我的标签看不到.

When the background color is set to clear, the program SIGABRTs. If I comment out that line, it works fine (except that the background for the label is just white). Also, if I just init the label rather than initWithFrame-ing it, the program doesn't crash, but my label can't be seen.

推荐答案

UIImageView *photo = [[[UIImageView alloc] initWithFrame:CGRectMake(title_bar.titleView.frame.origin.x+130, title_bar.titleView.frame.origin.y-12, 22.0, 22.0)] autorelease];
UIImage *theImage = [UIImage imageNamed:@"question mark icon"];
photo.image = theImage;
UIView *new_view = [[UIView alloc] init];
[new_view addSubview:photo];

title_bar.titleView = new_view;//this line by madhu

UILabel *new_label = [[UILabel alloc] initWithFrame:CGRectMake(title_bar.titleView.frame.origin.x-145, title_bar.titleView.frame.origin.y-12, 268.0, 22.0)];
[new_label setText:@"gregrgtg"];
[new_label setBackgroundColor:[UIColor clearColor]]; //COMMENTING THIS LINE OUT MAKES IT WORK
[new_view addSubview:new_label];

原因:

你最后添加了你的标题栏的标题视图,但之前提到过.

u had added ur titlebar's titleview atlast but referred them before .

这篇关于设置 UILabel 背景颜色导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆