development

프로그래밍 방식으로 고정 공간과 유연한 스페이스 바 버튼 항목을 만드는 방법은 무엇입니까?

big-blog 2020. 7. 1. 07:35
반응형

프로그래밍 방식으로 고정 공간과 유연한 스페이스 바 버튼 항목을 만드는 방법은 무엇입니까?


UIBarButtonItems프로그래밍 방식으로 만들고이 고정 공간 항목을 버튼 사이에 배치 하고 싶습니다 .


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

빠른

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

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

기타


Swift 3에서는 UIBarButtonItem이라는 이니셜 라이저가 init(barButtonSystemItem:target:action:)있습니다. init(barButtonSystemItem:target:action:)다음과 같은 선언이 있습니다.

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

지정된 시스템 항목을 포함하는 새 항목을 초기화합니다.


UIBarButtonSystemItem를 포함하여 대부분의 경우 제공을 열거 done, play, add또는 cancel. 그러나 필요에 따라 flexibleSpace또는 fixedSpace경우를 선택할 수도 있습니다 .

flexibleSpace case에는 다음과 같은 선언이 있습니다.

다른 항목 사이에 추가 할 공백. 공간은 다른 항목간에 균등하게 분배됩니다. 이 값을 설정하면 다른 항목 속성이 무시됩니다.

fixedSpace case에는 다음과 같은 선언이 있습니다.

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


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:

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 ViewAssistant EditorShow Assistant Editor

enter image description here

참고URL : https://stackoverflow.com/questions/3663058/how-to-create-fixed-space-and-flexible-space-bar-button-items-programmatically

반응형