2016-09-22 105 views
0

我在,我需要一個低於上述添加三個標籤導航欄的中心,任何人都可以幫助一個項目?如何將三個標籤添加到導航欄?

我已經試過這個片段:

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

參閱此http:/ /stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title –

+0

你想多行文本或多個標籤 –

+0

是庵埠,我NEDD在另一個下面添加三個標籤 – Arun

回答

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

中心或跟隨sailendra答案 –

+0

paaka,坦克庵埠(valthukal) – Arun

+0

@Arun - 現在你有想法,定製urself –

0

如果u需要添加到定義navigationController您預先那麼

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

變化根據您的navigationController

標籤的大小

enter image description here

+0

但獲取此格式通過整個viewcontroller我需要它在一個viewcontroller只有 – Arun

+0

好吧我會更新代碼 – Koushik

+0

再次進入所有視圖控制器.... – Arun

0

你也可以做到這一點在下文中提到的方法:

採取故事板自定義視圖,並設置自動佈局

使用視圖titleview的導航欄

代碼:

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

來看屏幕截圖與自動佈局:

enter image description here

輸出:

enter image description here

希望它能幫助。

1

@Arun我剛纔修改Anbu.Karthik代碼,可以幫助你

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; 

輸出:

enter image description here

快樂編碼...

+0

你也對吧... @ sailendara – Arun