0

我有代碼喜歡,在標題欄我有3個按鈕,左搜索按鈕,右相機按鈕和選項按鈕,之間我有標題。navigationItem titleview按鈕之間的中心

我想要做的是,我想顯示在搜索按鈕和相機按鈕之間居中的標題,任何人都可以幫助我如何做到這一點。

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

回答

0

使用與導航欄相同的寬度和高度創建標題標籤。例如

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

這會將您的文本放在中間。

+0

我想要搜索按鈕和相機按鈕之間的標題中心,目前我的標題接近搜索按鈕或有時接近相機按鈕,由於該標題看起來不太好。 –

+0

我已經修改了我的答案,如上所示。希望這對你更有用。 – Gismay

相關問題