0

我正在尝试在我的自定义导航栏上实现两个按钮,一个用于自定义后退按钮,一个用于我的设置 UIViewController。当我第一次实现后退按钮时,它显示得很好并且工作得很好,我实现为 leftBarButtonItem。但是,当我尝试实现第二个 UIBarButtonItem 时,它与后退按钮重叠。

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen     mainScreen].bounds.size.width, 41)];
navBar.delegate = self;   
UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:@"Create QR(URL)"];
[navBar pushNavigationItem:topItem animated:NO];
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];    
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    
[button setImage:buttonImage forState:UIControlStateNormal];  

button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

topItem.leftBarButtonItem = customBarItem;
UIImage *buttonImageSettings = [UIImage imageNamed:@"setting.png"];
UIButton *buttonSetting = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImageSettings forState:UIControlStateNormal];

buttonSetting.frame = CGRectMake(220, 0, buttonImageSettings.size.width,  buttonImageSettings.size.height);

[buttonSetting addTarget:self action:@selector(settings)    forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customBarItemSetting = [[UIBarButtonItem alloc]   initWithCustomView:buttonSetting];

topItem.rightBarButtonItem = customBarItemSetting;    
[self.view addSubview:navBar];
4

1 回答 1

0

假设 iOS 5 以后添加您的自定义按钮:

[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects: customBarItem, customBarItemSetting, nil] animated:NO];

(来自问题:如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar?

于 2013-01-29T17:15:23.483 回答