【问题标题】:How to create Custom Navigation Bar like BestBuy App?如何创建像 BestBuy App 这样的自定义导航栏?
【发布时间】:2013-02-07 09:26:37
【问题描述】:

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

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

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

【问题讨论】:

  • 我认为他们没有使用自定义导航栏...这是简单的图像:)
  • 他们可能不会……我同意。我的回答对此太过分了..它最灵活,但我认为 aziz 的代码看起来更适合这个。 ..虽然我不知道背景分隔符

标签: ios objective-c uinavigationcontroller uinavigationbar


【解决方案1】:

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

然后通过使用initWithNavigationBarClass:toolBarClass: 初始化来告诉您的navigationController 使用该类

例如

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

【讨论】:

  • 如何告诉 navigationController 使用那个类?
  • 那么我将如何设置根控制器?
  • 非动画推送或设置 navi.viewControllers = @[rootVC]
  • 我不理解非动画推送的概念。这是正确的做法吗?
  • 不工作!!导航栏是黑色的! self.navigationController.navigationBar.tintColor = [UIColor redColor];颜色没有变!
【解决方案2】:

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;

【讨论】:

  • 是的,例如,如果您不需要分隔符...尽管它们可能在背景图像中...这适用于大多数情况
  • 当然,但是按钮不会在触摸时突出显示,你的答案是更好的选择。
猜你喜欢
  • 2019-04-19
  • 2012-12-12
  • 2019-12-03
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多