cocoa-touch - 导航栏中的多个标签

标签 cocoa-touch

我想在 iPhone 上创建一个类似于“正在播放”页面的 View ,并在导航栏中有 3 行文本。

我能找到的唯一方法是:

UINavigationBar *bar = [self.navigationController navigationBar];   
label = [[UILabel alloc] initWithFrame:CGRectMake(60, 2, 200, 14)];
label.tag = SONG_TAG;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.highlightedTextColor = [UIColor blackColor];
[bar addSubview:label];
[label release];

//Create album label
label = [[UILabel alloc] initWithFrame:CGRectMake(60, 17, 200, 12)];
label.tag = ALBUM_TAG;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:12];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentCenter;
label.highlightedTextColor = [UIColor blackColor];
label.textColor = HEXCOLOR(0xA5A5A5ff);
[bar addSubview:label];
[label release];

//Create artist label
label = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 12)];
label.tag = ARTIST_TAG;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:12];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentCenter;
label.highlightedTextColor = [UIColor blackColor];
label.textColor = HEXCOLOR(0xA5A5A5ff);
[bar addSubview:label];
[label release];

问题是我必须在 View 更改时删除它们。所以,在 -viewWillDisappear 我有:

UILabel *label;
label = (UILabel *)[self.navigationController.navigationBar viewWithTag:SONG_TAG];
[label removeFromSuperview];
label = (UILabel *)[self.navigationController.navigationBar viewWithTag:ALBUM_TAG];
[label removeFromSuperview];
label = (UILabel *)[self.navigationController.navigationBar viewWithTag:ARTIST_TAG];
[label removeFromSuperview];

我认为这样做的方法是制作一个包含 3 个标签的自定义 View ,并将其添加到标题 View 中。 (这里有一个问题 - 您只能在导航栏上的标题 View 点添加 1 个标签或 View )

self.navigationItem.titleView = newViewIMadeWithThreeLabels

最佳答案

下面的 UIView 代码对我来说没问题,你有两次使用相同的标签标签,可能是你的崩溃原因。

UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];

UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.text = @"first line";
label.highlightedTextColor = [UIColor blackColor];
[btn addSubview:label];
[label release];

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.text = @"second line";
label.highlightedTextColor = [UIColor blackColor];
[btn addSubview:label];
[label release];

self.navigationItem.titleView = btn;

关于cocoa-touch - 导航栏中的多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/354385/

相关文章:

ios - UIAlertController 半透明故障/错误

ios - iPhone 6/6+ 屏幕尺寸问题

iphone - 透明颜色在模拟器上有效,但在 iPhone 上变成黑色

objective-c - 获取所有对象属性的列表

iphone - 如何将对象转发到单独的 subview ?

iphone - 我可以在iTunes Connect的哪个位置上传大图像图标?

ios - sleep 周期 - 这是哪种后台模式?

iphone - UITableView 不滚动到单元格

ios - 在后台下载期间未调用我的 NSURLSessionDelegate 方法

iphone - 为什么[UIColor白色]和黑色的亮度相等