Several labels in the navigation bar

I would like to create a look similar to the “Now Playing” page on the iPhone and have 3 lines of text in the navigation bar.

The only way I could find is:

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];

The problem with this is that I have to remove them when changing the view. So in -viewWillDisappear I have:

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];

I think the way to do this is to create a custom view with three labels in it and add it to the title view. (here's the trick - you can add only 1 shortcut or look in the name field of the navigation bar)

self.navigationItem.titleView = newViewIMadeWithThreeLabels
+3
source share
7 answers

The UIView code below works fine for me, you had the same tag tag that was used twice, could be the cause of your failure.

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;
+4

UIViewController , , UIViewControllerSubclass * sub = [[UIViewControllerSubclass alloc] init];

UIViewController loadView , [[self view] addSubview: label];

:

self.navigationItem.titleView = sub.view;

.

+2

. UIView, 3 titleView. subviews UINavigationBar.

+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];
+1

, .

  • UIView 320 * 44 0,0 - , .

  • IBOutlet navigationBarTitleView -

  • subview navgiationBar WillAppear: OR viewDidAppear:

    [[[ ]] : addSubview: navigationBarTitleView];

  • , navigationBarTitleView .

    [navigationBarTitleView removeFromSuperview];

+1

I tried to add the whole view. However, the application continued to crash when I added a request (e.g. a label) to the loadView delegate. I'm not so good at debugging complete crashes, so I found that the button was much simpler. I have not found any side effects yet ...

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;
0
source

An easier way is to enumtag:

enum {
  SONG_TAG,
  ALBUM_TAG,
  ARTIST_TAG
};

Then your code should work.

0
source

All Articles