如何创建像BestBuy App这样的自定义导航栏?

时间:2013-02-07 09:26:38

标签: ios objective-c uinavigationcontroller uinavigationbar

我想在BestBuy应用程序中创建一个自定义导航栏,或者如下面给出的截图所示。

enter image description here

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

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

2 个答案:

答案 0 :(得分:11)

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

然后告诉你的navigationController使用initWithNavigationBarClass:toolBarClass:

启动它来使用该类

e.g。

@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;
}

答案 1 :(得分: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;