UIBarButtonItem在自定义的UINavigationBar中相互重叠

时间:2013-01-29 17:05:11

标签: iphone ios uinavigationbar uibarbuttonitem uinavigationitem

我试图在我的自定义导航栏上实现两个按钮,一个用于自定义后退按钮,另一个用于我的设置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];

1 个答案:

答案 0 :(得分:0)

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

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

(来自问题:How to add 2 buttons into the UINavigationbar on the right side without IB?