【问题标题】:Set toolbar color and navigationbar back button设置工具栏颜色和导航栏后退按钮
【发布时间】:2013-12-12 13:37:59
【问题描述】:

我想在iOS7中设置工具栏的背景颜色: 我用这个设置颜色:

toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,40)];
[toolBar setBarStyle:UIBarStyleBlack];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,flexible,barButtonOther,nil];
toolBar.tintColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]];

但是背景图片没有显示。

我想知道这是做什么的:

 NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
    self.navigationController.navigationBar.translucent = NO;
} else {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];

}

Nd如何更改默认导航栏的返回按钮颜色:

【问题讨论】:

  • Toolbar 或 NavigationBar 它们是两个完全不同的东西。导航栏位于顶部,工具栏通常位于底部。
  • @Popeye 我知道..但我想知道该代码会做什么?
  • 我实际上是在谈论你的形象。如果它在顶部,它应该是一个导航栏而不是工具栏。
  • 但它不在顶部它在底部并且导航栏的代码不同我只是想知道该代码的含义
  • 好的,这只是你放置图像的方式,它看起来像状态栏>>工具栏>>视图。我的错误对不起。有关该代码的解释,请参阅我的答案。

标签: ios iphone objective-c ios7


【解决方案1】:

在 iOS 7 中你必须使用toolbar.barTintColor 设置颜色,toolbar.tintColor 将设置工具栏内 barButtons 的颜色。

最后一段代码测试应用程序是否在iOS 7上运行,如果是barTintColor,则使用tintColor,如果不是,则使用tintColor

要更改后退按钮,您可以通过设置导航栏的tintColor 来做同样的事情

【讨论】:

  • 我这样做了,然后我的完成和其他按钮消失了;
  • 我在工具栏上使用tintColorbarTintColor 的颜色相同,你的按钮和工具栏的颜色将相同。
  • 不,不,你是对的……我错误地删除了将按钮添加到工具栏的行……:)
  • 对于后退按钮我已经这样做了......但没有奏效。 self.navigationItem.backBarButtonItem.tintColor=[UIColor whiteColor];
  • self.navigationController.navigationBar.tintColor = [UIColor whiteColor] 应该这样做:)
【解决方案2】:

这段代码基本上是确定iOS是什么并选择正确的方法调用来设置导航栏的tintColor。请参阅下面代码中的 cmets

/* This is basically getting the systemVersion of the device so 7.0.1 - as this is returned
   as an NSString the user is separating this string based on the string "." which will
   place each string into the array. so you will have 3 objects in this array of "7", "0", "1"
 */
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
/* Once we have separated this string into the array we are getting the first object in that array
   so index `0` which will return a string at which point we are converting the string "7" to
   an intValue and comparing it to the int 7. So if that value is 7 or greater return TRUE
 */
if ([[ver objectAtIndex:0] intValue] >= 7) {
    /* The value was TRUE so lets use the iOS 7 way of setting the tintColor.
       We do this by setting barTintColor for the navigationBar and because
       in iOS 7 the navigationBar can be translucent - by default this is YES
       but because we are setting to a color we want we need to set this to NO
       because we don't want to have it translucent.
     */
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
    self.navigationController.navigationBar.translucent = NO;
} else {
    /* 
     If all fails in the if statements conditional check we must be on something 
     that is below iOS 7 so use the old way of things and just set the tintColor
     */
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
}

只是让您知道这不起作用。如果你打算使用这个,把它改成:

if (floor(NSFoundationVersionNumber) => NSFoundationVersionNumber_iOS_7_0) {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
    self.navigationController.navigationBar.translucent = NO;
} else {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
}

这是因为if ([[ver objectAtIndex:0] intValue] >= 7) 将返回 FALSE,即使 iOS 7 也是如此。

要设置工具栏,您可以使用相同的方法但替换:

self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
self.navigationController.navigationBar.translucent = NO;

[toolbar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]]];
[toolbar setTranslucent:NO];

并替换

 self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];

 [toolbar setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]]];

【讨论】:

  • 太棒了...如果我的应用程序仅针对 ios7,则无需检查操作系统版本...直接使用 bartintcolor ??
  • 这是对的,如果您只支持 iOS 7+ 并且您的部署目标是 iOS 7,那么就不需要 if statement,因为 else 部分永远不会在一百万年后达到。所以是的,你可以设置barTintColor:
【解决方案3】:
//How to set color of  *all* navigationbars and toolbars in an app in Swift 2.0

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

      func application(application: UIApplication, didFinishLaunchingWithOptionslaunchOptions: [NSObject: AnyObject]?) -> Bool {
         UINavigationBar.appearance().tintColor = UIColor.greenColor(); // All back button in the app are now green.
         UIToolbar.appearance().tintColor = UIColor.orangeColor(); // All toolBar icons in the app are now orange.

        //........
        let returnCode: Bool = getAppSpecificReturnCode();



       return returnCode
    }


    }

【讨论】:

    【解决方案4】:

    您可以使用以下代码...

    [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2017-12-07
      • 2017-03-27
      相关资源
      最近更新 更多