Pregunta Múltiples UIBarButtonItems en UINavigationBar


¿Cómo crear un botón de barra múltiple en la barra de navegación?


24
2018-02-24 05:45


origen


Respuestas:


Debe usar UIToolbar y configurar la barra de herramientas con botones:

// create a toolbar where we can place some buttons
UIToolbar *toolbar = [[UIToolbar alloc]
                        initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];

// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemSave
    target:self
    action:@selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];

// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
    target:nil
    action:nil];
[buttons addObject:spacer];

// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
    target:self
    action:@selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];

// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];

// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                           initWithCustomView:toolbar];

48
2018-02-24 05:47



Desde iOS 5 en adelante, ahora puedes hacerlo usando setLeftBarButtonItems:animated: o setRightBarButtonItems:animated:


75
2017-11-28 04:29



    you have to create a view with as much button you required and have to add them on navigation button like following :

    UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        UIButton *infoButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 30, 32)];
        [infoButton1 setBackgroundImage:[UIImage imageNamed: @"navbtn.png"] forState:UIControlStateNormal];
        [infoButton1 setTitle:@"Back" forState:UIControlStateNormal];
        infoButton1.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        infoButton1.titleLabel.textColor = [UIColor whiteColor];
        [infoButton1 addTarget:self action:@selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [parentView1 addSubview:infoButton1];
        [infoButton1 release];

UIButton *infoButton2 = [[UIButton alloc] initWithFrame:CGRectMake(30, 6, 30, 32)];
        [infoButton2 setBackgroundImage:[UIImage imageNamed: @"navbtn.png"] forState:UIControlStateNormal];
        [infoButton2 setTitle:@"Back" forState:UIControlStateNormal];
        infoButton2.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        infoButton2.titleLabel.textColor = [UIColor whiteColor];
        [infoButton2 addTarget:self action:@selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [parentView1 addSubview:infoButton2];
        [infoButton2 release];
        UIBarButtonItem *customBarButtomItem1 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
        [parentView1 release];
        self.navigationItem.leftBarButtonItem = customBarButtomItem1;
        [customBarButtomItem1 release];`enter code here`

3
2018-02-24 06:39



Sé que esta pregunta ya estaba cerrada, pero me parece que UIToolbar la solución no concuerda visualmente

Si en cambio usas un segundo UINavigationBar establecer con un UINavigationItem que tiene un título de nil y los botones deseados, puede agregar más botones y tener una barra que coincida visualmente con el original.


2
2018-04-09 22:47



- (void)viewWillAppear
{

    // get a view and :
    [self.navigationController.navigationBar addSubView:yourView];

}

0
2018-02-26 04:57



Para iOS7 y superior, esta es la forma correcta de hacerlo. No hay necesidad de tonterías UIToolbar.

- (void)viewDidLoad {

    [super viewDidLoad];
    [self configureView];

    // create three funky nav bar buttons
    UIBarButtonItem *one = [[UIBarButtonItem alloc]initWithTitle:@"One" style:UIBarButtonItemStylePlain target:self action:@selector(testMethod)];
    UIBarButtonItem *two = [[UIBarButtonItem alloc]initWithTitle:@"Two" style:UIBarButtonItemStylePlain target:self action:@selector(testMethod)];
    UIBarButtonItem *three = [[UIBarButtonItem alloc]initWithTitle:@"Three" style:UIBarButtonItemStylePlain target:self action:@selector(testMethod)];

    // create a spacer
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    space.width = 30;

    NSArray *buttons = @[one, space, two, space, three];

    self.navigationItem.rightBarButtonItems = buttons;
}

enter image description here

Odio poner enlaces como respuestas en SO, ya que pueden morir en cualquier momento, así que agregué un código relevante tomado de AQUÍ


0
2017-09-12 16:27