UINavigationBar:appearance works but not UINavigationBar:appearanceWhenContained in

后端 未结 2 1919
失恋的感觉
失恋的感觉 2021-02-06 13:00

I have a requirement to set the navigation bar to a custom color and this following code will do that:

[[UINavigationBar appearance]
            setBackgroundIma         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-06 13:20

    Indeed, like @rickster said, the appearanceWhenContainedIn: method customizes the appearances for instances of a class contained WITHIN an instance of a container class, or instances in a hierarchy.

    Not in every case you have a set of contained class you want to customize, but different containers. The solution to be able to customize several components is simply by creating an array of the classes you need to customize, and iterate! Like so:

    NSArray *navigationClass = [NSArray arrayWithObjects:[BSNavigationController class], [DZFormNavigationController class], nil];
    
    for (Class class in navigationClass)
    {
        //// Customize all the UINavigationBar background image tilling
        [[UINavigationBar appearanceWhenContainedIn:class, nil] setBackgroundImage:[UIImage imageNamed:@"yourImage"] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearanceWhenContainedIn:class, nil] setTintColor:[UIColor blackColor]];
    
        // Title Text Attributes
        NSDictionary *titleAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor], UITextAttributeTextColor,
                                         [UIColor darkGrayColor], UITextAttributeTextShadowColor,
                                         [UIFont boldSystemFontOfSize:20.0], UITextAttributeFont,
                                         [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,nil];
    
        //// Customize all the UINavigationBar title attributes
        [[UINavigationBar appearanceWhenContainedIn:class, nil] setTitleTextAttributes:titleAttributes];
    }
    

提交回复
热议问题