Objective c UINavigationBar和新的iOS 5+;外观API-如何提供两个背景图像?

Objective c UINavigationBar和新的iOS 5+;外观API-如何提供两个背景图像?,objective-c,ios,ios5,uinavigationbar,appearance,Objective C,Ios,Ios5,Uinavigationbar,Appearance,我想利用新的iOS 5外观API为我的应用程序中的所有UINavigationBar实例提供自定义背景图像。要做到这一点,很简单: [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault]; 但是,对于每个实例,我希望根据半透明属性的值提供不同的图像,例如 // For UINavigationBar insta

我想利用新的iOS 5外观API为我的应用程序中的所有UINavigationBar实例提供自定义背景图像。要做到这一点,很简单:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault];
但是,对于每个实例,我希望根据
半透明属性的值提供不同的图像,例如

// For UINavigationBar instances where translucent returns YES:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever-translucent.png"] forBarMetrics:UIBarMetricsDefault];

// Otherwise:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault];

假设外观API似乎是使用类方法配置的,那么这样做可能吗?

您可以使用类外观代理全局设置外观API,也可以在导航栏的实例上设置外观API

我目前正在为导航栏的一个实例设置背景,它似乎正在工作。我有两个不同背景的导航栏。如果在实例上设置它,则应该能够对代码进行条件设置

UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController:myView];
[viewControllers addObject:myNavController];

// not supported on iOS4
UINavigationBar *navBar = [myNavController navigationBar];
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
    // right here, you could condition bg image based on properties of this instance
    // of the navBar or any other condition.

    [navBar setBackgroundImage:[UIImage imageNamed:@"bg.jpg"] forBarMetrics:UIBarMetricsDefault];
}
如果要使用类方法进行设置,可以为所有设置:

[[UINavigationBar appearance] setBackground ...

我承认,这是一个非常新的问题,我和大多数人一样只是想弄明白而已。

目前,没有办法做到您所描述的——外观代理在您调用它时对任何特定实例一无所知

实际上,你可能需要做的是计算出你会有多少个半透明条。你有多少个非半透明的。选择你有更多的,并使用该一个外观代理-对于其他,当你去使其半透明(或要求全屏布局),你必须设置背景图像然后


同时,你能根据你的要求在提交一份增强请求吗?这不是一个不合理的要求。谢谢

这个答案可能对你没有多大帮助,但对其他人可能有帮助。如果创建子类,可以分别指定每个子类的外观。例如,我有UITableviewCells和一个从UITableviewCells派生的自定义类。实际上,我这样做是有原因的,但我发现我需要专门为这两个类调用[[UITableViewCells外观]setFont:[…]


因为您似乎希望基于一个直到运行时才知道的变量来执行此操作,所以您可能运气不好

如果您知道哪些类包含半透明条,您可以这样做:

[[UIBarButtonItem appearanceWhenContainedIn:[MyClassWithTranslucentBar class], [MyOtherClassWithTranslucentBar class], nil]
    setTintColor:desiredColor];

我不能留下评论,所以必须要有答案。Rob Whitlow就此写了一篇很棒的文章。查看它:

试试这个:

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {


        // Load resources for iOS 6.1 or earlier

         navigationController1 = [self customizedNavigationController];
         [navigationController1 setViewControllers:[NSArray arrayWithObject: self.homeViewController]];

         [self setNavigationController:navigationController1];
         [self.window setRootViewController:navigationController];


    } else {
        // Load resources for iOS 7 or later
         navigationController1 = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
          [self.window setRootViewController:navigationController1];
    }


  - (UINavigationController *)customizedNavigationController {

     UINavigationController *navController = [[UINavigationController alloc]   initWithNibName:nil bundle:nil];

    // Ensure the UINavigationBar is created so that it can be archived. If we do not access the
    // navigation bar then it will not be allocated, and thus, it will not be archived by the
    // NSKeyedArchvier.
    [navController navigationBar];

    // Archive the navigation controller.
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:navController forKey:@"root"];
    [archiver finishEncoding];

    // Unarchive the navigation controller and ensure that our UINavigationBar subclass is used.
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    [unarchiver setClass:[LNTNavigationBar class] forClassName:@"UINavigationBar"];
    UINavigationController *customizedNavController = [unarchiver decodeObjectForKey:@"root"];
    [unarchiver finishDecoding];

    // Modify the navigation bar to have a background image.
    LNTNavigationBar *navBar = (LNTNavigationBar *)[customizedNavController navigationBar];
    [navBar setTintColor:[UIColor colorWithRed:0.39 green:0.72 blue:0.62 alpha:1.0]];
    [navBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_1024_46.png"] forBarMetrics:UIBarMetricsDefault];
    [navBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_1024_46.png"] forBarMetrics:UIBarMetricsLandscapePhone];

    return customizedNavController;
}

if(楼层(NSFoundationVersionNumber)不过,我正试图在全局范围内实现这一点,对于任何UINavigationBar实例,实例方法都不会剪切它-本质上,我想使用class方法,但当外观代理应用于每个实例时,它会根据半透明属性的值设置不同的图像…我想我可能运气不好…我不知道除了在实例级别上这样做之外,还有一种方法可以做到这一点——这确实会让你乱扔你的代码,但你可以有一个通用的方法来做到这一点。我看到的另一件事是包含时的外观,但我认为这对你也没有帮助,因为这更多的是关于它控制使用哪种外观的视图类型……你知道HT考虑UINavigationBar的一个子类,UnavigigalBuxLuxor或任何,并使用外观包含在类方法。仍然是一种痛苦,但你不必“乱扔”你的代码特定的例子。是的,这是我在过去几分钟里所想的…谢谢。