简体   繁体   中英

left bar button items in navigationBar

In iOS5 we have leftBarButtonItems that we can set to a navigation bar, how can I do this for pre-iOS 5? I basically have an array of UIBarButtonItem that I wanted to set the bar to.

You can build own bar, and add it as left button:

    UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:@"First" style:UIBarButtonItemStyleBordered target:self action:@selector(firstButtonAction:)];
    UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:@"Second" style:UIBarButtonItemStyleBordered target:self action:@selector(secondButtonAction:)];

    UIToolbarTransparent *toolbar = [UIToolbarTransparent new];

    [toolbar setFrame:CGRectMake(0,0, 140,44)];
    [toolbar setItems:[NSArray arrayWithObjects:firstButton, secondButton, nil]];

    UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
    self.navigationItem.leftBarButtonItem = customBarButton;

UIToolbarTransparent

.h

#import <Foundation/Foundation.h>

@interface UIToolbarTransparent : UIToolbar {

}

.m

#import "UIToolbarTransparent.h"

@implementation UIToolbarTransparent

- (id)init {
    if (self = [super init]) {
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];
        self.translucent=YES;
    }
    return self;
}
@end

samfisher是对的,但您可以使用包含UIButtons的自定义UIView,而不是将UIView用作(单个)leftBasButtonItem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM