Odpowiedz

57

Oto jak to zrobić w kodzie, jeśli ktoś napotka tego postu:

UIBarButtonItem *leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem1Pressed:)] autorelease]; 

UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease]; 

UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem2Pressed:)] autorelease]; 


self.toolbarItems = [NSArray arrayWithObjects: leftButton, flex, rightButton, nil]; 
29

Wstaw element, którego identyfikatorem jest "przestrzeń elastyczna".

+0

Brzmi to lepszym rozwiązaniem niż moje! – bpapa

+0

Działa jak urok! Świetne rozwiązanie. –

+0

Po wykonaniu tej czynności przycisk nie pojawia się na ekranie. –

-1

Jeśli można uciec z tego nie będąc UIBarButtonItem (na przykład, przycisk INFO), co mogę zrobić, to zmienić warstwę przycisk, aby zawsze być z przodu na pasku narzędzi. W ten sposób, gdy przeciągnę przycisk nad paskiem narzędzi w IB, nie będzie on automatycznie wysysał go na lewą stronę.

8

Kod Swift:

func addDoneButton() -> UIToolbar { 
    let toolbar = UIToolbar() 
    let flexButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: Selector("donePressed")) 
    toolbar.setItems([flexButton, doneButton], animated: true) 
    toolbar.sizeToFit() 
    return toolbar 
} 
27

- jeden element w prawo jak ten

enter image description here

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: flexible, item1, nil]; 

-dwa elementy lewo i prawo jak tego

enter image description here

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2” style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, nil]; 

-Trzy przedmiotów, takich jak ten

enter image description here

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2” style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3” style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil]; 

Four przedmioty, takie jak ten

enter image description here

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2” style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3” style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithTitle:@"item4” style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, item3, flexible, item4, nil]; 

więc jeśli chcesz dodać spację należy dodać elastycznego przycisku bar.

0

Swift 3.x lub powyżej:

internal var textFieldHandlerToolBar: UIToolbar = { 
     let tb = UIToolbar.init(frame: CGRect.init(origin: .zero, size: CGSize.init(width: UIScreen.screenSize().width, height: 44.0))) 
     let flexibleButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) 
     let doneBarButton = UIBarButtonItem.init(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(actionDonePickerSelection)) 
     tb.setItems([flexibleButton, doneBarButton], animated: false) 
     return tb 
    }() 

wyjściowa:

enter image description here

Powiązane problemy