objective-c - 为 backBarButtonItem 创建自定义标题 View

标签 objective-c ios cocoa-touch uinavigationitem

我正在制作一个带有 PNG 的自定义 backBarButtonItem,效果很好。唯一的问题是如何更改文本颜色。我在导航栏上的做法是这样的:

CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"aColor.png"]];
self.navigationItem.titleView = label;
label.text = @"aTitle";

最佳答案

不要使用 autorelease 将标签设置为 titleview,分配后释放它们

        CGRect frame = CGRectMake(0, 0, 400, 44);

        UILabel *label = [[UILabel alloc] initWithFrame:frame];

        label.backgroundColor = [UIColor clearColor];

        label.font = [UIFont boldSystemFontOfSize:20.0];

        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

        label.textAlignment = UITextAlignmentCenter;

        label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"aColor.png"]];

        label.text = @"aTitle";

        self.navigationItem.titleView = label;

        [label release];

关于objective-c - 为 backBarButtonItem 创建自定义标题 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6552073/

相关文章:

ios - UITableView Section Index 重叠行删除按钮

ios - 自定义 UITableViewCell 初始值设定项在 IOS 中出错

ios - 在 objective-c 中使用类层次结构时如何从父类(super class)调用子类方法

android - 我可以使用后台任务每 1 秒处理一次来自服务器的数据吗?

iphone - 使用 CGPathRelease 释放路径导致 BAD_ACCESS

ios - 范围字符串 : always returns the same range for a recurring character

iOS:如何获取两个坐标之间的路线路径

iphone - UIDatePicker 显示秒数

ios - 将 iOS 应用程序连接到云数据库

ios - 摆脱警告 "Expression Result Unused"