【问题标题】:Customise UI Navigation Bar and button自定义 UI 导航栏和按钮
【发布时间】:2016-03-30 13:46:12
【问题描述】:

你好,我是ios开发新手,想知道如何自定义导航栏按钮

这是我用来改变导航栏颜色的方法

[[UINavigationBar appearance] setBarTintColor:[UIColor]]

我想改变按钮颜色

这是一个例子

【问题讨论】:

  • 按钮颜色还是条形颜色?
  • 按钮颜色和使按钮像图片

标签: ios objective-c user-interface ios7 uikit


【解决方案1】:

首先你需要设置导航栏,假设你的第一个屏幕是SpalshViewController,你需要将启动屏幕设置为导航栏的根视图控制器,然后将导航栏设置为根视图控制器:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SplashScreenVC *obj =[CustomUtility getViewController:@"SplashScreenVC"];
self.navigationController =[[UINavigationController alloc] initWithRootViewController:obj];
self.window.rootViewController =self.navigationController;

didFinishLaunchingWithOptions

现在来自定义导航栏设置和您需要设置的外观:

self.navigationController.navigationBar.translucent = NO;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"top_bg2.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"Lato-Regular" size:18]}];

现在为导航栏设置自定义栏按钮,您需要创建 barbutton 项目,例如:

UIBarButtonItem *leftbtn;
UIBarButtonItem *rightbtn;

[self.navigationController.navigationItem setLeftBarButtonItem:leftbtn];
[self.navigationController.navigationItem setRightBarButtonItem:rightbtn];

自定义barbuttonItems根据你的需要

如果你想在导航栏上设置多个按钮,你可以添加如下:

UIBarButtonItem *leftbtn1;
UIBarButtonItem *leftbtn2;
UIBarButtonItem *rightbtn1;
UIBarButtonItem *rightbtn2;
[self.navigationController.navigationItem setLeftBarButtonItems:@[leftbtn1,leftbtn2]];
[self.navigationController.navigationItem setRightBarButtonItems:@[rightbtn1,rightbtn2]];

希望对你有帮助

【讨论】:

    【解决方案2】:

    您可以使用下面的导航栏进行自定义。在 Storyboard 中,您可以选择更改颜色。

    否则你可以把 UIView 自定义。如下图,

    【讨论】:

      【解决方案3】:

      使用您想要的颜色创建 1x1 像素图像。然后将以下代码添加到您的 AppDelegate 类中。

      UIImage *btnBgColor = [[UIImage imageNamed:@"backColor"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
      
      [[UIBarButtonItem appearance] setBackgroundImage: btnBgColor 
                                              forState:UIControlStateNormal
                                            barMetrics:UIBarMetricsDefault];
      

      在您的基础 viewController 类上编写以下代码,然后将该类作为所有其他视图控制器的超类。

      UIButton* yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
      yourButton.layer.backgroundColor = [UIColor redColor].CGColor;
      
      UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc] initWithCustomView: yourButton];
      self.navigationBar.leftBArButtonItems = @[buttonItem];
      

      【讨论】:

      • 如何使用ui颜色?
      【解决方案4】:

      您可以在导航栏中添加自定义按钮,例如:

          UIButton *btnObj = [UIButton buttonWithType:UIButtonTypeCustom];
          [btnObj setFrame:CGRectMake(0, 0, 60, 44)];
          [btnObj setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
          [btnObj setImage:[UIImage imageNamed:@"<button background image>"] forState:UIControlStateNormal];
          [btnObj addTarget:self action:@selector(btnObjClick:) forControlEvents:UIControlEventTouchUpInside];
          UIBarButtonItem *itemBtnObj = [[UIBarButtonItem alloc] initWithCustomView:btnObj];
          self.navigationItem.rightBarButtonItems = [[NSArray alloc]initWithObjects:itemBtnObj, nil];
      

      【讨论】:

        【解决方案5】:

        您可以从故事板本身进行。 拖动 UINavigation Bar

        使用图像或背景颜色自定义您的导航,给它们阴影。

        添加导航项

        您也可以根据需要使用图像或颜色以及图像和位置来自定义它们。

        要添加相同的约束,请参阅我的 ans。 Adding constraints to a UIBarButtonItem

        希望它有所帮助.. 快乐编码.. :)

        【讨论】:

          猜你喜欢
          • 2016-04-01
          • 2011-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多