在IOS 7中将标题设置为UIToolBar

cdu*_*dub 8 uitoolbar uibarbuttonitem ios ios7

我在下面声明了一个UIToolBar按钮:

UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 64.0);
Run Code Online (Sandbox Code Playgroud)

我还有一个UIBarButtonItem作为后退按钮添加到左侧.但是,如何在UIToolbar中添加居中的标签标题?

Ant*_* MG 7

创建UILabel并将其添加为工具栏的项目:

UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:myLabel];
Run Code Online (Sandbox Code Playgroud)

如果要将其居中,请在侧面添加灵活空间:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
Run Code Online (Sandbox Code Playgroud)


Jan*_*aya 7

UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 20)];
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
lblTitle.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:lblTitle];
UIBarButtonItem *typeField = [[UIBarButtonItem alloc] initWithCustomView:lblTitle];   
toolBar.items = [NSArray arrayWithArray:[NSArray arrayWithObjects:backButton,spaceBar,lblTitle, nil]];
Run Code Online (Sandbox Code Playgroud)

这会解决你的问题.