如何以编程方式创build固定空间和灵活的空格键button项目?

我想以编程方式创buildUIBarButtonItems并在button之间放置这些固定的空间项目。

 UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; fixedItem.width = 20.0f; // or whatever you want UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
 UIBarButtonItem *todayItem = [[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)]; todayItem.tag = 2; UIBarButtonItem *cashItem = [[UIBarButtonItem alloc] initWithTitle:@"Cash" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)]; cashItem.tag = 3; UIBarButtonItem *creditItem = [[UIBarButtonItem alloc] initWithTitle:@"Credit" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)]; creditItem.tag = 4; UIBarButtonItem *allItem = [[UIBarButtonItem alloc] initWithTitle:@"All" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)]; allItem.tag = 1; UIBarButtonItem *returnItem = [[UIBarButtonItem alloc] initWithTitle:@"Return" style:UIBarButtonItemStylePlain target:self action:@selector(update_baritem:)]; returnItem.tag = 5; UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; [fixedItem setWidth:455.0f]; UIBarButtonItem *fixed2Item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; [fixed2Item setWidth:37.0f]; UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [self.toolbar setItems:@[fixed2Item, returnItem, creditItem, cashItem, fixedItem, todayItem, flexibleItem, allItem] animated:NO]; 

迅速

 // Fixed Space let fixedSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil) fixedSpace.width = 20.0 // Flexible Space let flexibleSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 

在ViewDidLoad中:

  //toolbar UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)]; // bar btns UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(goBack)]; UIBarButtonItem *forwardBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(goForward)]; UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem *bookmarkBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(bookmark)]; UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)]; UIBarButtonItem *stopLoadingBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(stopLoading)]; // add btns to the bar [toolBar setItems:[NSMutableArray arrayWithObjects:bookmarkBtn,backBtn,forwardBtn,flexibleSpace,refreshBtn,stopLoadingBtn, nil]]; // adds the toobar to the view [self.view addSubview:toolBar]; 

不要忘记每个button的操作(在这个例子中是一个UIWebView):

  -(void)goBack { [_webView goBack]; } -(void)goForward { [_webView goForward]; } 

等等

使用Swift 3, UIBarButtonItem有一个名为init(barButtonSystemItem:target:action:)init(barButtonSystemItem:target:action:)具有以下声明:

 convenience init(barButtonSystemItem systemItem: UIBarButtonSystemItem, target: Any?, action: Selector?) 

初始化包含指定系统项目的新项目。


UIBarButtonSystemItem是一个枚举,提供了很多情况,包括doneplayaddcancel 。 但是,根据您的需要,您也可以selectflexibleSpacefixedSpace案例。

flexibleSpace案例有以下声明:

在其他项目之间添加空格。 空间在其他项目之间平均分配。 当设置此值时,其他项目属性将被忽略。

fixedSpace案件有以下声明:

在其他项目之间添加空格。 设置此值时仅使用width属性。


因此,您可以编程方式创build固定和灵活的空格键button项目,如下所示:

 let flexibleSpace = UIBarButtonItem( barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil ) 
 let fixedSpace = UIBarButtonItem( barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil ) fixedSpace.width = 30 // Set width with the appropriate value 

例如,下面的Playground代码显示了如何在视图控制器中添加一个具有两个居中播放的底部条和由固定空间30分隔的暂停条button项:

 import PlaygroundSupport import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white title = "Home" } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Show navigation controller's built-in toolbar navigationController?.setToolbarHidden(false, animated: false) // Create UIBarButtonItems let flexibleSpace1 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let playItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: nil) let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) fixedSpace.width = 30 let pauseItem = UIBarButtonItem(barButtonSystemItem: .pause, target: self, action: nil) let flexibleSpace2 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) // Set the view controller toolbar items setToolbarItems([flexibleSpace1, playItem, fixedSpace, pauseItem, flexibleSpace2], animated: false) } override func viewWillDisappear(_ animated: Bool) { // Hide navigation controller's built-in toolbar navigationController?.setToolbarHidden(true, animated: true) super.viewWillDisappear(animated) } } let viewController = ViewController() let navigationController = UINavigationController(rootViewController: viewController) PlaygroundPage.current.liveView = navigationController 

在“游乐场”助理编辑器中使用“ 查看” ▸“ 助理编辑器” ▸“ 显示助理编辑器”

在这里输入图像说明