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

发布于 2024-12-09 15:13:53 字数 694 浏览 9 评论 0原文

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

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

但是,对于每个实例,我想根据 translucent 属性的值提供不同的图像,例如,

// 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 似乎是使用配置的类方法,这样的事情可能吗?

I want to exploit the new iOS 5 appearance API to supply custom background images to all UINavigationBar instances in my app. To do this, it's as simple as this:

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

However, for each instance, I want to provide a different image depending on the value of the translucent property, e.g.

// 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];

Given that the appearance APIs seem to be configured using class methods, is something like this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ぃ弥猫深巷。 2024-12-16 15:13:53

目前,无法执行您所描述的操作 - 外观代理在您调用它时对任何特定实例一无所知。

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

同时,您能否在 http://bugreport.apple.com/ 上针对您的需求提交增强请求在问吗?这并不是一个无理的要求。谢谢!

At the moment, there's no way to do what you're describing - the appearance proxy doesn't know anything about any particular instance at the time you're calling for it.

In practical terms, what you'll probably need to do is figure out how many translucent bars you'd have v. how many non-translucent ones you had. Choose whichever you have more of and use the appearance proxy for that one - for the others, when you go to make it translucent (or ask for full-screen layout), you'll have to set the background image then.

In the meantime, could you file an enhancement request at http://bugreport.apple.com/ for what you're asking? It's not an unreasonable request. Thanks!

梦里°也失望 2024-12-16 15:13:53

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

我目前正在导航栏的一个实例上设置背景,它似乎正在工作。我有两个不同背景的不同导航栏。如果您在实例上设置它,您应该能够调整代码。

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 ...

我承认它是相当新的,我只是像大多数人一样弄清楚它。

You can either set it globally with the class appearance proxy or set it on an instance of a navBar.

I'm currently setting background on an instance of the nav bar and it seems to be working. I have two different navBars with different backgrounds. If you set it on an instance, you should be able to condition the code.

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

If you want to set using the class method, you can set for all:

[[UINavigationBar appearance] setBackground ...

I will admit though that it's pretty new and I'm just figuring it out like most folks.

青瓷清茶倾城歌 2024-12-16 15:13:53

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

由于您似乎想要基于一个直到运行时才知道的变量来执行此操作,因此您可能不走运!

This answer probably won't be of much help to you, but it may be to others. IF you make a subclass, you can specify the appearance for each subclass separately. For instance, I have UITableviewCells and a custom class that is derived from UITableViewCells. I actually do this for a reason, but I discovered that i need to call [[UITableViewCells appearance] setFont:[...]] for both classes specifically.

Since you seem to want to do so based upon a variable that you will not know until runtime, you are probably out of luck!

电影里的梦 2024-12-16 15:13:53

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

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

You can do it like this if you know which classes contain the translucent bars:

[[UIBarButtonItem appearanceWhenContainedIn:[MyClassWithTranslucentBar class], [MyOtherClassWithTranslucentBar class], nil]
    setTintColor:desiredColor];
欢你一世 2024-12-16 15:13:53

我无法发表评论,所以必须给出答案。罗布·惠特洛(Rob Whitlow)就此写了一篇很棒的文章。查看:http://ios-blog。 co.uk/tutorials/ios-custom-ui-series-tabbar-navbar/

I cant leave a comment so will have to be an answer. Rob Whitlow wrote a great article on this. Check it out: http://ios-blog.co.uk/tutorials/ios-custom-ui-series-tabbar-navbar/

泛滥成性 2024-12-16 15:13:53

试试这个:

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

Try this:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文