如何在导航栏中添加三个标签?

时间:2016-09-22 10:35:27

标签: ios objective-c uinavigationbar

我正在开展一个项目,我需要将上面三个标签添加到导航栏的中心,任何人都可以帮忙吗?

我试过这个片段:

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

4 个答案:

答案 0 :(得分: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

快乐的编码......

答案 1 :(得分:1)

Some ~> Id

答案 2 :(得分:0)

如果您需要添加到预定义的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

答案 3 :(得分: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;

使用Autolayout进行视图的屏幕截图:

enter image description here

输出

enter image description here

希望它有所帮助。