Bagaimana cara membuat item tombol ruang tetap dan ruang fleksibel tombol terprogram?

134

Saya ingin membuat secara UIBarButtonItemsterprogram dan menempatkan item-item ruang tetap ini di antara tombol.

Dunia Linux
sumber

Jawaban:

296
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];
Jerry Jones
sumber
44
Perhatikan bahwa untuk mengatur lebar UIBarButtonItem Ruang Tetap, Anda perlu mengatur .widthproperti.
Nick Forge
jawaban sempurna!
Pratik Jamariya
bagaimana kita memiliki lebar fleksibel memberikan jarak yang sama antara semua tombol menggunakan kode? Saya tidak ingin hard code width = 20.0
Satyam
Cukup letakkan satu lebar fleksibel di antara masing-masing dua tombol @ Satyam
fl034
15

Cepat

// 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)
365SplendidSuns
sumber
7
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];
hans
sumber
Mengapa Anda mengatur item tombol bilah kanan ke item kiri ? Itu hanya gaya atau bug yang benar-benar buruk dan saya tidak tahu yang mana.
Ben Lachman
1

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

Jangan lupa tindakan untuk setiap tombol juga (dalam contoh ini UIWebView):

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

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

dll.

SENI LAOMUSIK
sumber
1

Dengan Swift 3, UIBarButtonItemmemiliki inisialisasi yang disebut init(barButtonSystemItem:target:action:). init(barButtonSystemItem:target:action:)memiliki deklarasi berikut:

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

Menginisialisasi item baru yang mengandung item sistem yang ditentukan.


UIBarButtonSystemItemadalah pencacahan yang menawarkan banyak kasus termasuk done, play, addatau cancel. Namun, sesuai dengan kebutuhan Anda, Anda juga dapat memilih flexibleSpaceatau fixedSpacekasing.

flexibleSpace case memiliki deklarasi berikut:

Ruang kosong untuk menambahkan antara item lainnya. Ruang didistribusikan secara merata di antara barang-barang lainnya. Properti item lainnya diabaikan ketika nilai ini disetel.

fixedSpace case memiliki deklarasi berikut:

Ruang kosong untuk menambahkan antara item lainnya. Hanya widthproperti yang digunakan ketika nilai ini ditetapkan.


Karenanya, Anda dapat membuat item tombol bilah spasi tetap dan fleksibel secara terprogram seperti yang ditunjukkan di bawah ini:

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

Sebagai contoh, kode Playground di bawah ini menunjukkan cara menambahkan bilah bawah dengan dua item tombol bidik tengah dan jeda yang dipisahkan oleh ruang tetap 30 di pengontrol tampilan:

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

Pratinjau pengontrol tampilan Anda di editor asisten Playground menggunakan ViewAssistant EditorShow Assistant Editor

masukkan deskripsi gambar di sini

Imanou Petit
sumber
0

Cepat 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)
Mike Raccoon
sumber
0

Dalam Swift:

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