2012-10-13 12 views
12

ho creato una barra degli strumenti di programmazione:Come aggiungere un pulsante sul lato destro della barra degli strumenti?

UIToolbar *boolbar = [UIToolbar new]; 
    boolbar.barStyle = UIBarStyleDefault; 
    boolbar.tintColor = [UIColor orangeColor]; 
    [boolbar sizeToFit]; 

E poi ha aggiunto un pulsante per esso:

UIBarButtonItem *cancelleftBarButton =[[UIBarButtonItem alloc]initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(tapBackGround:)]; 

cancelleftBarButton.tintColor = [UIColor orangeColor]; 

NSArray *array = [NSArray arrayWithObjects:cancelleftBarButton, nil]; 
[boolbar setItems:array animated:YES]; 

Tuttavia, questo pulsante viene visualizzato solo sul lato sinistro della barra degli strumenti. È possibile metterlo sul lato destro della barra degli strumenti?

enter image description here

risposta

34

Ecco il metodo per aggiungere il UIBarButtonItem sul lato destro della barra degli strumenti.

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

O

Se si sta tentando di farlo dal XIB, allora.

Inserire un elemento che ha identificatore come "spazio flessibile".

enter image description here

+0

Verificate anche questi collegamenti http://stackoverflow.com/questions/602717/aligning-uitoolbar-items e http://stackoverflow.com/ domande/6021138/how-to-registrare-UIToolbar-sinistra-e-destra-padding – IronManGill

4

In Swift

let btn1 = UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Done, target: self, action: "btn1Pressed" 
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
let btn2 = UIBarButtonItem(title: "Button 2", style: UIBarButtonItemStyle.Done, target: self, action: "btn2Pressed") 
Problemi correlati