如何使用BACK按钮在UINavigationBar的LEFT侧添加多个UIBarButtonItem

时间:2011-04-27 10:09:52

标签: ios objective-c uinavigationbar uibarbuttonitem

我可以按照http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/在UINavigationBar的右侧添加多个按钮。但是,我找不到在左侧添加多个按钮的任何解决方案。我想在我的iPad应用程序的UINavigation Bar中的Back按钮旁边添加一个“Edit”按钮。它有点像iPad YouTube应用程序中的“注销”和“编辑”按钮。

任何人都知道如何实施它?

由于

3 个答案:

答案 0 :(得分:0)

您可以尝试在左侧添加UIView并以编程方式添加按钮,但似乎没有更好的方法。这可能是你列出的教程的工作原理,但我没有检查。如果您需要一个代码示例,我可以提供一个。

答案 1 :(得分:0)

这将解决您的问题,

UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];

UIButton *myButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton2 setImage:[UIImage imageNamed:@"Main_Menu_SearchIcon.png"] forState:UIControlStateNormal];
myButton2.frame = CGRectMake(0.0, 0.0, 70,30);
[myButton2 addTarget:self action:@selector(openSetting:) forControlEvents:UIControlEventTouchUpInside];
myButton.showsTouchWhenHighlighted = myButton2.showsTouchWhenHighlighted = YES;

UIButton *myButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton3 setImage:[UIImage imageNamed:@"Main_Menu_SearchIcon.png"] forState:UIControlStateNormal];
myButton3.frame = CGRectMake(50.0, 0.0, 70,30);
[myButton3 addTarget:self action:@selector(openSetting:) forControlEvents:UIControlEventTouchUpInside];
myButton.showsTouchWhenHighlighted = myButton3.showsTouchWhenHighlighted = YES;


[leftView addSubview:myButton2];
[leftView addSubview:myButton3];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:self.tabBarItem.title];
    UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    naviBarObj.barTintColor = [UIColor lightGrayColor];
    naviBarObj.items = [NSArray arrayWithObjects:item, nil];
    naviBarObj.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];

答案 2 :(得分:0)

 let editImage   = UIImage(named: "YourImageName1")!

 let searchImage = UIImage(named: "YourImageName2")!

let editButton   = UIBarButtonItem(image: YourImageName1,  style: 
.Plain, target: self, action: "didTapEditButton:")

let searchButton = UIBarButtonItem(image: YourImageName2,  style: 
 .Plain, target: self, action: "didTapSearchButton:")

navigationItem.leftBarButtonItems = [YourImageName1, YourImageName2]

最后为敲击按钮设置功能

func didTapEditButton(sender: AnyObject)

{

 ...
}

func didTapSearchButton(sender: AnyObject)

{

...
}