Chcę programowo utworzyć UIBarButtonItems
i umieścić te stałe elementy przestrzeni między przyciskami.Jak programowo ustawić elementy przycisku przestrzeni stałej i elementów spacji?
Odpowiedz
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];
Dlaczego ustawiasz * w prawo * elementy przycisku paska na * w lewo * elementy? To po prostu zły styl lub błąd i nie wiem, który. –
W 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];
Nie zapomnij czynności dla każdego przycisku także (w tym przykładzie UIWebView):
-(void)goBack
{
[_webView goBack];
}
-(void)goForward
{
[_webView goForward];
}
itp
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)
Z Swift 3 UIBarButtonItem
ma inicjator nazwie init(barButtonSystemItem:target:action:)
. init(barButtonSystemItem:target:action:)
ma następującą deklarację:
convenience init(barButtonSystemItem systemItem: UIBarButtonSystemItem, target: Any?, action: Selector?)
Inicjuje nową pozycję zawierającą określony element systemu.
UIBarButtonSystemItem
jest wyliczenie, który oferuje wiele przypadków, w tym done
, play
, add
lub cancel
. Jednak zgodnie z Twoimi potrzebami możesz również wybrać przypadki flexibleSpace
lub .
flexibleSpace
sprawa ma następującą deklarację:
Blank Space doda między innymi pozycjami. Przestrzeń jest równo rozdzielona między inne przedmioty. Inne właściwości elementu są ignorowane, gdy ta wartość jest ustawiona.
fixedSpace
sprawa ma następującą deklarację:
Blank Space doda między innymi pozycjami. Tylko własność
width
jest używana, gdy ta wartość jest ustawiona.
Dlatego można tworzyć spacja elementy stałe i elastyczne przycisk programowo, jak pokazano poniżej:
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
Jako przykład, kod zabaw poniżej pokazach jak dodać dolny pasek z dwoma wyśrodkowanymi przyciskami odtwarzania i pauzy paska przycisków oddzielonymi przez stałą przestrzeń wynoszącą 30 w vi Kontroler ew:
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
Podgląd kontroler widok na asystent montażysty Playground korzystając View ▸ asystent montażysty ▸ Pokaż Assistant Editor
- 1. Wyrównanie elementów za pomocą przestrzeni stałej (o zmiennej szerokości)
- 2. Jak programowo ustawić wagę dla przycisku
- 3. Jak ustawić programowo programowo?
- 4. Android - Jak programowo ustawić styl przycisku w Linearlayout?
- 5. Jak ustawić rozmiar przycisku?
- 6. Jak programowo ustawić windowDrawsSystemBarBackgrounds?
- 7. Jak przywrócić domyślne tło przycisku (programowo)?
- 8. Rails Niepowitanie stałej NameError (kolizja modelu i przestrzeni nazw)
- 9. Jak ustawić obraz tła przycisku poprzez kod
- 10. Highcharts - Jak programowo przełączanie elementów legendy i określić, które elementy są zaznaczone
- 11. Android Programowo ustawiający tekst przycisku
- 12. Sposób stylizowania przycisku menu i elementów menu
- 13. jak ustawić przycisk w stałej lokalizacji u dołu interfejsu użytkownika?
- 14. Usuwanie początkowych i końcowych spacji z elementów XML
- 15. Jak ustawić programowo kilka opcji
- 16. Jak programowo ustawić kalendarz ttk?
- 17. Jak ustawić program CacheMode na elemencie programowo?
- 18. Ustawianie obrazu tła przycisku iPhone programowo
- 19. Jak programowo ustawić rozmiar NSView?
- 20. Android jak ustawić windowanimation programowo
- 21. Jak programowo ustawić layout_alignParentRight = "false"?
- 22. iOS - Jak programowo ustawić UISwitch?
- 23. Android: Jak programowo ustawić drawablePadding?
- 24. jak ustawić wysokość gridview programowo Android
- 25. Jak zawijać tekst przycisku html o stałej szerokości
- 26. Dynamicznie utworzyć tablicę i ustawić elementy
- 27. Jak programowo pokazać zwinięte elementy w CoordinatorLayout?
- 28. Jak dodać elementy podmenu do elementów menu
- 29. Jak programowo ustawić niestandardową zmienną w Emacs Lisp?
- 30. ListView WPF - jak programowo dodawać elementy?
Należy pamiętać, że aby ustawić szerokość Fixed Space UIBarButtonItem, musisz ustawić właściwość '.width'. –
idealna odpowiedź! –
w jaki sposób możemy mieć elastyczną szerokość dać równe odstępy między wszystkimi przyciskami za pomocą kodu? Nie chcę twardej szerokości kodu = 20.0 – Satyam