如何在UINavigationBar上将自定义按钮设置在我想要的位置?

时间:2011-09-09 13:56:26

标签: iphone objective-c ios cocoa-touch ios4

我有一个使用UINavigationBar的视图

导航栏只允许我处于固定位置的按钮

导航栏两侧的固定位置..

我想自定义按钮的位置...... 任何想法......都会帮助我 在此先感谢

2 个答案:

答案 0 :(得分:1)

您无法自定义UINavigationItem上按钮的位置,您只能设置rightBarButtonItem和leftBarButtonItem。

如果您确实需要,请考虑使用工具栏。

如果您需要一个后退按钮,就像导航栏的后退按钮一样,创建一个自定义按钮并使用图像。

这是一个有用的PSD

答案 1 :(得分:0)

您可以在UINavigationItem中定位自定义按​​钮。这就是我在右边添加三个按钮的方法:

// create a toolbar to have three buttons in the right (thanks Mart!)
tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 157, 44.01)];

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 1.0, 157.0, 44.1)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
[tools addSubview:imgView];
[tools sendSubviewToBack:imgView];
[tools setTintColor:[UIColor colorWithRed:127/255.0 green:184/255.0 blue:72/255.0 alpha:1.0]];
[imgView release];

// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

UIBarButtonItem* bi = [[UIBarButtonItem alloc] 
                       initWithTitle:@"Filter" style:UIBarButtonItemStyleBordered target:self action:@selector(showFilter:)];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc]
      initWithImage:[UIImage imageNamed:@"Map.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showMap:)];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc] 
      initWithImage:[UIImage imageNamed:@"Favourite.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(saveSearch:)];
[buttons addObject:bi];
[bi release];

[tools setItems:buttons animated:NO];

[buttons release];

rightBarButton = nil;
rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:tools];

self.navigationItem.rightBarButtonItem = rightBarButton;