ios 如何以编程方式创建固定空间和灵活空格键按钮项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3663058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:41:44  来源:igfitidea点击:

How to create fixed space and flexible space bar button items programmatically?

iosiphonexcodeuibarbuttonitem

提问by Linux world

I want to create UIBarButtonItemsprogrammatically and place these fixed space items between buttons.

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

回答by Jerry Jones

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

回答by 365SplendidSuns

Swift

迅速

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

回答by hans

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

回答by LAOMUSIC ARTS

In ViewDidLoad:

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

Do not forget the actions for each button also(in this example a UIWebView):

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

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

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

etc.

等等。

回答by Imanou Petit

With Swift 3, UIBarButtonItemhas an initializer called init(barButtonSystemItem:target:action:). init(barButtonSystemItem:target:action:)has the following declaration:

在 Swift 3 中,UIBarButtonItem有一个名为init(barButtonSystemItem:target:action:). init(barButtonSystemItem:target:action:)有以下声明:

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

Initializes a new item containing the specified system item.

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



UIBarButtonSystemItemis an enumeration that offers many cases including done, play, addor cancel. However, according to your needs, you may also choose flexibleSpaceor fixedSpacecases.

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

flexibleSpacecase has the following declaration:

flexibleSpacecase 有以下声明:

Blank space to add between other items. The space is distributed equally between the other items. Other item properties are ignored when this value is set.

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

fixedSpacecase has the following declaration:

fixedSpacecase 有以下声明:

Blank space to add between other items. Only the widthproperty is used when this value is set.

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



Therefore, you can create fixed and flexible space bar button items programmatically as shown below:

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

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


As an example, the Playground code below shows how to add a bottom bar with two centered play and pause bar button items separated by a fixed space of 30 in a view controller:

例如,下面的 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


Preview your view controller in the Playground assistant editor using View? Assistant Editor? Show Assistant Editor

使用View?在 Playground 助手编辑器中预览您的视图控制器。Assistant Editor?Show Assistant Editor

enter image description here

在此处输入图片说明

回答by Mike Raccoon

Swift 5.1.2

斯威夫特 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)