0

I am working in a project in which i need to add Three Labels one below the above to the center of the navigation bar, Can anyone help ?

I have tried this snippet:

UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = @"My Text";
navLabel.textColor = [UIColor redColor];
[naviBarObj addSubview:navLabel];
[navLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:naviBarObj];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
jerfin
  • 803
  • 1
  • 13
  • 28

4 Answers4

1
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 110)];

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = @"My Text";
navLabel.textColor = [UIColor redColor];
[navLabel setBackgroundColor:[UIColor clearColor]];

UILabel *navLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(200,navLabel.frame.origin.y + navLabel.frame.size.height + 5,200,30)];
navLabel1.text = @"My Text1";
navLabel1.textColor = [UIColor redColor];
[navLabel1 setBackgroundColor:[UIColor clearColor]];

UILabel *navLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(200,navLabel1.frame.origin.y + navLabel1.frame.size.height + 5,200,30)];
navLabel2.text = @"My Text2";
navLabel2.textColor = [UIColor redColor];
[navLabel2 setBackgroundColor:[UIColor clearColor]];


[naviBarObj addSubview:navLabel];
[naviBarObj addSubview:navLabel1];
[naviBarObj addSubview:navLabel2];

[self.view addSubview:naviBarObj];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

@Arun I have just modify the Anbu.Karthik code which might help you

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 64.0f)];

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,2,200,18)];
navLabel.text = @"My Text";
navLabel.textColor = [UIColor redColor];
[navLabel setBackgroundColor:[UIColor clearColor]];
navLabel.textAlignment = NSTextAlignmentCenter;
[customView addSubview:navLabel];

UILabel *navLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0,navLabel.frame.size.height + 2,200,18)];
navLabel1.text = @"My Text1";
navLabel1.textColor = [UIColor redColor];
navLabel1.textAlignment = NSTextAlignmentCenter;
[navLabel1 setBackgroundColor:[UIColor clearColor]];
[customView addSubview:navLabel1];

UILabel *navLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0,navLabel.frame.size.height + navLabel1.frame.size.height + 2,200,18)];
navLabel2.text = @"My Text2";
navLabel2.textColor = [UIColor redColor];
navLabel2.textAlignment = NSTextAlignmentCenter;
[navLabel2 setBackgroundColor:[UIColor clearColor]];
[customView addSubview:navLabel2];

self.navigationItem.titleView = customView;

Output:

enter image description here

Happy coding...

Sailendra
  • 1,318
  • 14
  • 29
0

if u need to add to your pre defined navigationController then

 UILabel *tempLabel1 = [[UILabel alloc]init];
[tempLabel1 setFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, 12)];
[tempLabel1 setText:@"Text 1"];
[tempLabel1 setFont:[UIFont systemFontOfSize:12]];
[tempLabel1 setTextAlignment:NSTextAlignmentCenter];
[self.navigationController.navigationBar addSubview:tempLabel1];

UILabel *tempLabel2 = [[UILabel alloc]init];
[tempLabel2 setFrame:CGRectMake(0, 12, self.navigationController.navigationBar.frame.size.width, 24)];
[tempLabel2 setText:@"Text 2"];
[tempLabel2 setFont:[UIFont systemFontOfSize:12]];
[tempLabel2 setTextAlignment:NSTextAlignmentCenter];
[self.navigationController.navigationBar addSubview:tempLabel2];

UILabel *tempLabel3 = [[UILabel alloc]init];
[tempLabel3 setFont:[UIFont systemFontOfSize:12]];
[tempLabel3 setFrame:CGRectMake(0, 24, self.navigationController.navigationBar.frame.size.width, 36)];
[tempLabel3 setText:@"Text 3"];
[tempLabel3 setTextAlignment:NSTextAlignmentCenter];
[self.navigationController.navigationBar addSubview:tempLabel3];

change the size of label according to your navigationController

enter image description here

Koushik
  • 1,220
  • 15
  • 19
0

You can also do it in the below mentioned way :

Take a custom View in story board and set the auto layout

Use that view as titleView of navigation bar

Code :

//Here "NavigationView" is the name of view file (xib).
UIView *customVwNavigation = [[[NSBundle mainBundle] loadNibNamed:@"NavigationView" owner:self options:nil] objectAtIndex:0];

self.navigationItem.titleView = customVwNavigation;

Screen shot of view with Autolayout :

enter image description here

Output :

enter image description here

Hope it helps.

Community
  • 1
  • 1
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43