导航栏中有多个标签

时间:2008-12-09 21:57:19

标签: cocoa-touch

我想创建一个类似于iPhone上“正在播放”页面的视图,并在导航栏中有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];

这个问题是我必须在视图更改时删除它们。所以,在-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个标签的自定义视图,并将其添加到标题视图中。 (这是捕获 - 您只能在导航栏上的标题视图点添加1个标签或视图)

self.navigationItem.titleView = newViewIMadeWithThreeLabels

7 个答案:

答案 0 :(得分:4)

下面的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;

答案 1 :(得分:2)

您可以尝试创建一个正确定位标签的UIViewController,然后实例化它,如UIViewControllerSubclass * sub = [[UIViewControllerSubclass alloc] init];

在UIViewController子类中,您必须在loadView委托方法中构建视图,并将每个标签的视图附加到视图控制器的视图,如[[self view] addSubview:label];

然后附上它的视图:

self.navigationItem.titleView = sub.view;

这可能有效。

答案 2 :(得分:1)

您在帖子中的后续建议是正确的。创建一个包含3个标签的UIView,并将其设置为titleView。请勿尝试将子视图直接添加到UINavigationBar。

答案 3 :(得分:1)

UINavigationBar *bar = [self.navigationController navigationBar];
CGFloat navBarHeight = 70.0f;    
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[bar setFrame:frame];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 320, 10)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:12];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.text = @"Set the details for this event.";
label.highlightedTextColor = [UIColor blackColor];
[bar addSubview:label];
[label release];
[bar release];

答案 4 :(得分:1)

今天我遇到了同样的问题,我这样做了。

  1. 我在位置0,0创建了一个大小为320 * 44的UIView - 根据您的要求,您可以拥有不同的视图大小。

  2. 为名为navigationBarTitleView的自定义视图创建IBOutlet - 与您的视图建立必要的连接

  3. 在viewWillAppear中使用以下代码将子视图添加到navgiationBar:OR viewDidAppear:

    [[[self navigationController] navigationBar] addSubview:navigationBarTitleView];

  4. 当您在navigationcontroller上推送此视图时,请从superview中删除navigationBarTitleView。

    [navigationBarTitleView removeFromSuperview];

答案 5 :(得分:0)

我尝试添加整个视图..但是,每当我在loadView委托中添加子视图(如标签)时,应用程序都会崩溃。我调试完全崩溃并不是那么好,所以我发现使用按钮要简单得多。我还没有找到任何副作用......

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 320, 60);

UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 200, 16)];
label.tag = SONG_TAG;
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 = SONG_TAG;
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;

答案 6 :(得分:0)

一种更简洁的方法就是enum你的标签:

enum {
  SONG_TAG,
  ALBUM_TAG,
  ARTIST_TAG
};

您的代码应该可以使用。