如何创建固定空间和灵活的空格栏按钮项目编程?

我想以编程方式创建 UIBarButtonItems,并将这些固定的空间项放置在按钮之间。

77232 次浏览
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];

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

不要忘记每个按钮的操作(在本例中是 UIWebView) :

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


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

等等。

斯威夫特

// 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)

对于 Swift 3,UIBarButtonItem有一个名为 init(barButtonSystemItem:target:action:)的初始化器:

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

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


UIBarButtonSystemItem 是一个列举,提供了许多情况,包括 doneplayaddcancel。但是,根据您的需要,您也可以选择 flexibleSpacefixedSpace的情况下。

flexibleSpace 案件有以下声明:

其他项之间添加的空白。空间在其他物品之间平均分配。设置此值时将忽略其他项属性。

fixedSpace 案件有以下声明:

其他项之间添加的空白。设置此值时只使用 width属性。


因此,可以通过编程方式创建固定和灵活的空格栏按钮项,如下所示:

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个固定空间隔开:

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

在 Playground 助理编辑器中使用 View预览视图控制器

enter image description here

Swift 5.1.2

// Fixed Space
let fixedSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
fixedSpace.width = 20.0


// Flexible Space
let flexibleSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

斯威夫特:

let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
fixedSpace.width = 20.0

在写这篇文章的时候,如果你的目标是 iOS14或更高版本,你可以使用相应的 class 函数来得到更简洁的固定和灵活的空间项:

let fixedSpace = UIBarButtonItem.fixedSpace(20)
let flexibleSpace = UIBarButtonItem.flexibleSpace()

医生: 固定空间灵活的空间