这就是我如何更改导航栏的背景并尝试像这样设置字体
UIImage *image = [UIImage imageNamed:@"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
[tmpTitleLabel font:[UIFont systemFontOfSize:12]];
// APP CRASH (IF I ERASH THIS ABOVE LINE THEN TITLE GET DISPLAYED AS FACEBOOK )
tmpTitleLabel.text = @"Facebook";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
[self.navigationController.navigationBar addSubview:newView];
我做错了什么?我检查了很多答案,但这就是他们设置字体的方式。
最佳答案
[tmpTitleLabel 字体:[UIFont systemFontOfSize:12]];
这是错误的。您正在使用 getter 而不是 setter。您错过了 set
部分和大写 F
。
[tmpTitleLabel setFont:[UIFont systemFontOfSize:12]];
^^^^
或者像这样使用点语法:
tmpTitleLabel.font = [UIFont systemFontOfSize:12];
关于ios - 更改字体时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16099761/