我有三个UIBarButtonItem
创建如下.它们左对齐,我想对齐中心,所以右侧没有间隙.我没有看到对齐属性UIToolBar
.还有另一种方法来实现这一目标吗?
//create some buttons UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(showAbout:)]; [toolbar setItems:[NSArray arrayWithObjects:settingsButton,deleteButton,aboutButton,nil]]; //Add the toolbar as a subview to the navigation controller. [self.navigationController.view addSubview:toolbar];
nduplessis.. 258
将两个UIBarButtonSystemItemFlexibleSpace项添加到工具栏中,项目的左侧和右侧
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, settingsButton,deleteButton,aboutButton, flexibleSpace, nil]];
像任何其他工具栏项一样添加这些项将在两者之间均匀分配空间.
将两个UIBarButtonSystemItemFlexibleSpace项添加到工具栏中,项目的左侧和右侧
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, settingsButton,deleteButton,aboutButton, flexibleSpace, nil]];
像任何其他工具栏项一样添加这些项将在两者之间均匀分配空间.
这也可以直接从故事板中完成.
只需拖放工具栏中的项目,然后将其中的一些项目转换为灵活或固定的空间即可获得所需的效果.请参阅以下两个示例.
在Xamarin iOS中
右对齐:
yourBar.SetItems(new [] { new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), yourButton }, false);
中心对齐:
var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace); yourBar.SetItems(new [] { flexibleSpace, yourButton, flexibleSpace}, false);
Swift版本:
let toolbar = UIToolbar(frame: CGRectMake(0, 0, viewController.view.frame.size.width, 35.0)) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: viewController, action: nil) let button1 = UIBarButtonItem(title: "A", style: UIBarButtonItemStyle.Plain, target: viewController, action: foo) let button2 = UIBarButtonItem(title: "B", style: UIBarButtonItemStyle.Plain, target: viewController, action: bar) let button3 = UIBarButtonItem(title: "C", style: UIBarButtonItemStyle.Plain, target: viewController, action: blah) toolbar.items = [button1, flexibleSpace, button2, flexibleSpace, button3]