我想在 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/