如何创build自定义导航栏,如BestBuy应用程序?
我想要像BestBuy应用程序或像下面给出的屏幕截图所示的那样制作自定义导航栏。
我希望这种types的导航始终位于每个viewController的顶部。
如果有人能告诉我的程序或任何forms的帮助,将不胜感激。 谢谢。
编写一个UINavigationBar的子类,在其中进行自定义绘图并根据需要添加子视图。
那么通过使用initWithNavigationBarClass:toolBarClass:
来启动它,告诉你的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; }
navigationBar可以带三个视图,左右两边各有两个button,还可以为标题添加一个视图,
//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];
并添加search栏
self.navigationItem.titleView = mySearchbarView;