6

我想制作一个自定义导航栏,就像在 BestBuy 应用程序中一样,或者像下面给出的屏幕截图所示。

在此处输入图像描述

我希望这种导航始终位于每个 viewController 的顶部。

如果有人能告诉我程序或任何形式的帮助,将不胜感激。谢谢。

4

2 回答 2

11

编写 UINavigationBar 的子类,您可以在其中进行自定义绘图并根据需要添加子视图。

然后告诉你的navigationController通过使用初始化它来使用那个类initWithNavigationBarClass:toolBarClass:

例如

@interface MyBar : UINavigationBar
@end

@implementation MyBar 
.... //like any UIView
@end

UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[MyBar class] toolbarClass:nil];

代替initWithRootViewController


样本

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPhone" bundle:nil];
} else {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPad" bundle:nil];
}

UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[UINavigationBar class] toolbarClass:nil];
navi.viewControllers = @[self.mainViewController];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
return YES;
}
于 2013-02-07T09:32:54.243 回答
4

navigationBar 可以取三个视图,左右各有两个按钮,也可以为标题添加一个视图,,

//this will set a background image to your navigation bar.
[[self.navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:@"top-bar.png"] forBarMetrics:UIBarMetricsDefault];

UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setBackgroundImage:[UIImage imageNamed:@"backBtn.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 66, 30);
[leftButton addTarget:self action:@selector(navBack) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

UIImage *image=[UIImage imageNamed:@"add-btn.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height);
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(doneAct) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

并添加搜索栏

self.navigationItem.titleView = mySearchbarView;
于 2013-02-07T09:36:30.853 回答