Comment pouvons-nous insert plusieurs titres sur UInavigationBar dans ios

Salut je suis débutant dans iOS et dans mon projet j'ai besoin d'insert "titres" à "centre gauche" et "centre droit" sur UInavigationBar comme ci-dessous image s'il vous plaît aidez-moi comment dois-je faire en dessous de mes exigences

entrez la description de l'image ici

Voir le code ci-dessous et essayez:

- (void)viewDidLoad { [super viewDidLoad]; CGRect frame = [[UIScreen mainScreen] bounds]; UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)]; lbl1.backgroundColor = [UIColor redColor]; lbl1.textAlignment = NSTextAlignmentCenter; lbl1.text = @"Dashboard"; [self.navigationController.navigationBar addSubview:lbl1]; UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)]; lbl2.backgroundColor = [UIColor redColor]; lbl2.textAlignment = NSTextAlignmentCenter; lbl2.text = @"Profile"; [self.navigationController.navigationBar addSubview:lbl2]; } 

Vous pouvez personnaliser le titleView de la barre de navigation. Lisez la documentation officielle ici .

Selon votre question, j'ai essayé le code de travail ci-dessous: –

 -(UIView *)getTitleView { CGRect frame = [[UIScreen mainScreen] bounds]; UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)]; UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)]; lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f]; lbl1.textAlignment = NSTextAlignmentCenter; [lbl1 setTextColor:[UIColor whiteColor]]; lbl1.text = @"Dashboard"; [viewTitle addSubview:lbl1]; UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)]; lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f]; [lbl2 setTextColor:[UIColor whiteColor]]; lbl2.textAlignment = NSTextAlignmentCenter; lbl2.text = @"Profile"; [viewTitle addSubview:lbl2]; return viewTitle; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.titleView = [self getTitleView]; } 

Je divise le code en méthode séparée pour suivre une approche modulaire . Pour toute partie du code, qui est une tâche distincte, écrivez ce morceau de code dans une méthode différente. Cela conduira à plus de lisibilité.