Objective c 导航项目标题查看按钮之间的居中对齐

Objective c 导航项目标题查看按钮之间的居中对齐,objective-c,uinavigationcontroller,uibarbuttonitem,uinavigationitem,Objective C,Uinavigationcontroller,Uibarbuttonitem,Uinavigationitem,我有类似的代码,在标题栏中我有3个按钮,左搜索按钮,右摄像头按钮和选项按钮,中间我有标题 我想做的是,我想显示在搜索按钮和相机按钮之间居中的标题,有人能帮我怎么做吗 //Left Button UIBarButtonItem *leftSearch = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_search"] style:UIBarButtonItemStyleBordered target:self ac

我有类似的代码,在标题栏中我有3个按钮,左搜索按钮,右摄像头按钮和选项按钮,中间我有标题

我想做的是,我想显示在搜索按钮和相机按钮之间居中的标题,有人能帮我怎么做吗

//Left Button
UIBarButtonItem *leftSearch = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_search"] style:UIBarButtonItemStyleBordered target:self action:@selector(gotoSearch)];

leftSearch.imageInsets = UIEdgeInsetsMake(0, -5.0f, 0, 0);

[leftSearch setTintColor:[UIColor whiteColor]];
/////////

//Right Button -- First Camera
UIBarButtonItem *rightCamera = [[UIBarButtonItem alloc]
                                initWithImage:[UIImage imageNamed:@"icon_camera"]
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(gotoPhotoGallery)];
[rightCamera setTintColor:[UIColor whiteColor]];
rightCamera.imageInsets = UIEdgeInsetsMake(0, 0, 0, -59.0f);
///////////////////

//Right second Button -- for options menu
UIBarButtonItem *rightMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header_option"] style:UIBarButtonItemStyleBordered target:self action:@selector(gotoPhotoGallery)];
[rightMenu setTintColor:[UIColor whiteColor]];
rightMenu.imageInsets = UIEdgeInsetsMake(0, 0, 0, 5.0f);
///////////////////

NSArray *buttons = @[rightMenu, rightCamera];

self.navigationItem.rightBarButtonItems = buttons;

[[self navigationItem] setLeftBarButtonItem:leftSearch];

//Title Label

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Museo Sans" size:20];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = LoginUserName;
self.navigationItem.titleView = label;
[label sizeToFit];

创建与导航栏具有相同宽度和高度的标题标签。比如说

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,44)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = LoginUserName;
titleLabel.textColor = [UIColor whiteColor];
[self.navigationController.navigationBar addSubview:titleLabel];

这将把你的文本放在中间。

我希望标题中心位于搜索按钮和相机按钮之间,目前我的标题靠近搜索按钮,或者有时它靠近相机按钮,因为标题看起来不太好。我已按上述方式修改了我的答案。希望这对你更有用。