【问题标题】:iOS 7 UIBarButton back button arrow coloriOS 7 UIBarButton 后退按钮箭头颜色
【发布时间】:2013-08-25 10:08:17
【问题描述】:

我正在尝试更改后退按钮箭头

我目前正在使用以下方法来控制文本大小以及后退按钮上的文本颜色:

[[UIBarButtonItem appearance] setTitleTextAttributes:
  [NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor whiteColor], UITextAttributeTextColor,
    [UIFont boldSystemFontOfSize:16.0f], UITextAttributeFont,
    [UIColor darkGrayColor], UITextAttributeTextShadowColor,
    [NSValue valueWithCGSize:CGSizeMake(0.0, -1.0)], UITextAttributeTextShadowOffset,
  nil] forState:UIControlStateNormal];

但如果我只想更改后退按钮的箭头颜色,我该怎么办?

【问题讨论】:

  • 您找到任何解决方案来更改后退按钮的箭头颜色了吗?
  • @OMK 我最终更改了我的 infolistproperty NavBarColor 以使其工作,然后将实际的 navbarcolor 设置为不同的颜色。我不确定发生了什么,但该解决方案对我有用
  • UINavigationBar 的某些属性的行为已从 iOS 7 更改。查看Answer 以了解其他一些 属性 的效果。
  • 请帮忙解决这个问题:stackoverflow.com/questions/29923813/…

标签: ios ios7


【解决方案1】:

您可以在按钮(或条形按钮项)或视图控制器的视图上设置tintColor 属性。默认情况下,该属性将从父视图继承色调,一直到您应用的顶级 UIWindow

【讨论】:

  • 感谢您的回复。但是,它在 iOS 7 上的工作方式略有不同。我的旧代码使用的是 tintColor,但 iOS 7 似乎不太喜欢它。我最终使用了官方的 Apple Dev iOS 7 过渡指南来修复一些 UI 问题
  • 查看我的答案以获取有关如何执行此操作的离散示例:stackoverflow.com/a/18809412/1103584
【解决方案2】:

你必须设置整个应用的tintColor。

self.window.tintColor = [UIColor redColor];

或者在 Swift 3 中:

self.window?.tintColor = UIColor.blue

来源:iOS 7 UI Transition Guide

【讨论】:

  • 如果您只想设置后退按钮人字形的颜色(而不是整个应用程序),如原始问题所述,这是错误的答案。在这里查看我的答案以获得正确答案:stackoverflow.com/a/18809412/1103584
  • 我在我的应用程序委托中使用它来将我的后退按钮更改为黑色。放在 applicationDidFinishLaunchingWithOptions 方法中... window.tintColor = [UIColor blackColor];
  • 这不能在 iOS 6 上运行。有人知道该怎么做吗?
  • 这导致它影响整个应用程序的 tintColor,其中包括任何 UIActivityViewController(用于共享)和 MFMailComposeViewController(用于发送电子邮件)。这些对话框假定 tintColor 没有被修改过......并且可能导致一些难看的颜色交互。
  • 这会改变许多其他对象的色调颜色,例如UITextField 中闪烁的光标。绝对不推荐。
【解决方案3】:

在 iOS 6 中,tintColor 为导航栏、标签栏、工具栏、搜索栏和范围栏的背景着色。要在 iOS 7 中为条形背景着色,请改用 barTintColor 属性。

iOS 7 Design Resources iOS 7 UI Transition Guide

【讨论】:

  • 就是这个! ...把它放在应用程序委托中。奇迹般有效! window.tintColor = [UIColor blackColor]; // 我想要黑色
【解决方案4】:

更改特定导航控制器的后退按钮 V 形颜色*:

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

*如果您正在使用具有多个导航控制器的应用程序,并且您希望此 V 形颜色应用于每个,您可能希望使用外观代理为每个导航控制器设置后退按钮 V 形,如下所示:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

而且,为了更好的衡量,迅速(感谢 cmets 中的 Jay Mayu):

UINavigationBar.appearance().tintColor = UIColor.whiteColor()

【讨论】:

  • 如果您为整个应用程序使用相同的导航控制器,这将仅适用于整个应用程序。我实际上推荐巴特的建议。
  • 我用关于多个导航控制器的注释更新了我的答案。我仍然不同意 Bart 的建议是“正确”的答案,因为当您设置整个应用程序的色调颜色时,还有很多其他副作用。另外,最初的问题并没有问如何为整个应用设置主题,而只是问如何为后退按钮设置主题。
  • 这似乎没有设置背面人字形的颜色。
  • @jcampbell1 - 我在我的几个应用程序中成功使用了它......也许你没有阅读关于多个 UINavigationControllers 的警告。
  • for swift UINavigationBar.appearance().tintColor = UIColor.whiteColor()
【解决方案5】:

您可以使用该方法设置整个应用导航栏的颜色

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions{
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}

【讨论】:

    【解决方案6】:

    这样可以只改变箭头的颜色(而不是后退按钮标题的颜色):

    [[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor blackColor]];
    

    导航栏包含 _UINavigationBarBackIndicatorView 类型的子视图(子视图数组中的最后一项),代表箭头。

    结果是navigation bar with different colors of back button arrow and back button title

    【讨论】:

    • 依赖系统视图的子视图顺序是个坏主意。
    • 这有效,当设置 self.navigationController.navigationBar.tintColor 不起作用。
    • @GlennMaynard 你说得对——实现这一点的最佳方法是遍历self.navigationController.navigationBar.subviews 返回的子视图数组,直到找到后退按钮。
    • @EvanR 虽然 selmad 的解决方案确实对我有用,但遍历子视图数组却没有(我首先尝试过,因为我同意你的观点,这样做是明智的)。如果它适合您 - 请添加一个示例。谢谢。
    【解决方案7】:

    在初始化 navigationController 的 rootViewController 中,我将这段代码放在了我的 viewDidAppear 方法中:

    //set back button color
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
    //set back button arrow color
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    

    【讨论】:

    • UITextAttributeTextColor 在 iOS 7 中已弃用 - 使用 NSForegroundColorAttributeName
    【解决方案8】:

    只需更改NavigationBar 颜色,您可以如下设置色调颜色。

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    

    【讨论】:

    • 这不会影响 iOS 7 中的色调颜色。根据文档:“iOS 7 不支持使用外观代理 API 设置 tintColor 属性。” link
    【解决方案9】:

    我不得不同时使用:

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] 
                         setTitleTextAttributes:[NSDictionary 
                   dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] 
                                       forState:UIControlStateNormal];
    
    [[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor whiteColor]];
    

    为我工作,谢谢大家!

    【讨论】:

      【解决方案10】:

      在 iOS 7 中,您可以将以下代码行放入您的 application:didFinishLaunchingWithOptions: 文件中的 AppDelegate.m 中:

      [[UINavigationBar appearance] setTintColor:myColor];
      

      myColor 设置为您希望后退按钮在整个应用程序中显示的颜色。无需将其放在每个文件中。

      【讨论】:

        【解决方案11】:

        如果您基于带有箭头图像的 UIButton 制作自定义后退按钮,这里是子类 sn-p。 使用它,您可以在代码中创建按钮,也可以将 Interface Builder 中的类分配给任何 UIButton。 后退箭头图像将自动添加并用文本颜色着色。

        @interface UIImage (TintColor)
        
        - (UIImage *)imageWithOverlayColor:(UIColor *)color;
        
        @end
        
        
        @implementation UIImage (TintColor)
        
        - (UIImage *)imageWithOverlayColor:(UIColor *)color
        {
            CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
        
            if (UIGraphicsBeginImageContextWithOptions) {
                CGFloat imageScale = 1.0f;
                if ([self respondsToSelector:@selector(scale)])
                    imageScale = self.scale;
                UIGraphicsBeginImageContextWithOptions(self.size, NO, imageScale);
            }
            else {
                UIGraphicsBeginImageContext(self.size);
            }
        
            [self drawInRect:rect];
        
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetBlendMode(context, kCGBlendModeSourceIn);
        
            CGContextSetFillColorWithColor(context, color.CGColor);
            CGContextFillRect(context, rect);
        
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        
            return image;
        }
        
        @end
        
        
        
        
        #import "iOS7backButton.h"
        
        @implementation iOS7BackButton
        
        -(void)awakeFromNib
        {
            [super awakeFromNib];
        
            BOOL is6=([[[UIDevice currentDevice] systemVersion] floatValue] <7);
            UIImage *backBtnImage = [[UIImage imageNamed:@"backArrow"] imageWithOverlayColor:self.titleLabel.textColor];
            [self setImage:backBtnImage forState:UIControlStateNormal];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, is6?0:-10, 0, 0)];
        
        
        }
        
        
        + (UIButton*) buttonWithTitle:(NSString*)btnTitle andTintColor:(UIColor*)color {
            BOOL is6=([[[UIDevice currentDevice] systemVersion] floatValue] <7);
            UIButton *backBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
            UIImage *backBtnImage = [[UIImage imageNamed:@"backArrow"] imageWithOverlayColor:color];
            [backBtn setImage:backBtnImage forState:UIControlStateNormal];
            [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, is6?5:-5, 0, 0)];
            [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, is6?0:-10, 0, 0)];
            [backBtn setTitle:btnTitle forState:UIControlStateNormal];
            [backBtn setTitleColor:color /*#007aff*/ forState:UIControlStateNormal];
        
            return backBtn;
        }
        
        @end
        

        【讨论】:

          【解决方案12】:
          UINavigationBar *nbar = self.navigationController.navigationBar;
          
          if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
             //iOS 7
             nbar.barTintColor = [UIColor blueColor]; // bar color
             //or custom color 
             //[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
          
             nbar.navigationBar.translucent = NO;
          
             nbar.tintColor = [UIColor blueColor]; //bar button item color
          
          } else {
             //ios 4,5,6
             nbar.tintColor = [UIColor whiteColor];
             //or custom color
             //[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
          
          }
          

          【讨论】:

          • 太棒了。它帮助了我。谢谢
          【解决方案13】:

          如果您只想更改整个应用程序上的后退箭头,请执行以下操作:

          [[NSClassFromString(@"_UINavigationBarBackIndicatorView") appearance] 
            setTintColor:[UIColor colorWithHexString: @"#f00000"]];
          

          【讨论】:

          • 这可能会让你被应用商店拒绝
          • @Luke,我已经成功使用过几次了。没有拒绝。但是,是的,机会仍然存在。
          • 在以后的iOS版本中还是有可能会坏的
          • @Lope,是的,为什么不呢。我不是在寻找银弹。
          • 遍历 iOS 10.1 上的子视图显示这仍然是类名,但设置它的外观如这里所示对我没有任何影响。
          【解决方案14】:

          如果您使用情节提要,您可以设置导航栏的色调。

          【讨论】:

          • 正是我想要的。谢谢!
          【解决方案15】:

          Swift 2.0:为导航栏和按钮着色

          navigationController?.navigationBar.barTintColor = UIColor.blueColor()
          navigationController?.navigationBar.tintColor = UIColor.whiteColor()
          navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
          

          【讨论】:

            【解决方案16】:

            在 swift 3 中,更改 UIBarButton 后退按钮箭头颜色

            self.navigationController?.navigationBar.tintColor = UIColor.black
            

            【讨论】:

              【解决方案17】:

              更新 Swift 3

              navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.yellow
              navigationController?.navigationBar.tintColor = UIColor.red
              navigationController?.navigationBar.barTintColor = UIColor.gray
              navigationController?.navigationBar.titleTextAttributes =  [NSForegroundColorAttributeName: UIColor.blue]
              

              结果:

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-09-27
                • 2017-11-13
                • 1970-01-01
                • 2020-02-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-12-16
                相关资源
                最近更新 更多