2012-10-13 248 views
12

he creado una barra de herramientas de programación:¿Cómo agregar un botón en el lado derecho de la barra de herramientas?

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

Y luego añade un botón a la misma:

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

Sin embargo, este botón aparece solamente en el lado izquierdo de la barra de herramientas. ¿Es posible colocarlo en el lado derecho de la barra de herramientas?

enter image description here

Respuesta

34

Aquí es el método para añadir el UIBarButtonItem en el lado derecho de la barra de herramientas.

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

Si usted está tratando de hacerlo desde el XI ter, a continuación.

Inserta un elemento cuyo identificador sea "espacio flexible".

enter image description here

+0

También puedes ver estos enlaces http://stackoverflow.com/questions/602717/aligning-uitoolbar-items y http://stackoverflow.com/ preguntas/6021138/how-to-adjust-uitoolbar-left-and-right-padding – IronManGill

4

En 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") 
Cuestiones relacionadas