我有代码喜欢,在标题栏中我有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];
答案 0 :(得分: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];
这会将你的文字放在中间。