web-dev-qa-db-fra.com

iOS 7: désactiver la translucidité UINavigationBar pour toute l'application

Existe-t-il un moyen de désactiver UINavigationBar Translucency pour une application entière?

Je sais que l'utilisation de [self.navigationController.navigationBar setTranslucent:NO] peut résoudre ce problème pour un seul contrôleur, mais j'ai beaucoup d'UINavigationBars dans mon application et c'est une solution assez fastidieuse.

J'ai essayé [[UINavigationBar appearance] setTranslucent:NO], mais cette fonctionnalité n'est étonnamment pas prise en charge. Cela se traduit par Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:'

Si je dois le faire, je peux parcourir l'ensemble de mon application en définissant UINavigationBars pour désactiver la translucidité une par une, mais il doit y avoir une solution plus élégante à ce problème ...

54
MikeS

si vous définissez la translucidité de la première barre de navigation de la pile sur false[self.navigationController.navigationBar setTranslucent:NO], il se reflétera dans tous les NavigationViewController suivants qui sont poussés vers cette pile.

33
Roshan

Voici une solution Swift si vous souhaitez appliquer ce style à l'ensemble de l'application.

dans la classe AppDelegate ajoutez ceci au didFinishLaunchingWithOptions:

Pour Swift 2:

UINavigationBar.appearance().translucent = false

Pour Swift 3:

UINavigationBar.appearance().isTranslucent = false
16
dan

Cela semble très simple avec ce code dans appDelegatedidFinishLaunchingWithOptions (fonctionne très bien avec iOS 8 et versions supérieures)

[[UINavigationBar appearance] setTranslucent:NO];
7
Sudhin Davis

Je pense que vous avez raison sur le fait qu'aucun proxy d'apparence n'est disponible pour cette propriété. Utilisez-vous des objets UINavigationControllers ou UINavigationBar? Si vous utilisez UINavigationBars, vous pouvez le sous-classer et créer une barre de navigation non translucide.

En tête de fichier:

#import <UIKit/UIKit.h>

@interface ABCNonTranslucentNavBar : UINavigationBar

@end

Fichier d'implémentation:

#import "ABCNonTranslucentNavBar.h"

@implementation ABCNonTranslucentNavBar

- (void)drawRect:(CGRect)rect
{
  [self setTranslucent:NO];
}

Remplacez ensuite simplement les UINavigationBars par votre sous-classe. Vous pouvez également faire quelque chose de similaire avec un UINavigationController sous-classé.

3
Kyle Clegg

Ajout de cela au cas où quiconque se bat toujours contre cela.

Vous pouvez le tromper cependant en spécifiant une image qui n'existe pas, ce qui rendra la barre de navigation INCLUANT sa barre d'outils opaque

[[UIToolbar appearance] setBackgroundColor:[UIColor colorWithRed:219.0/255.0 green:67.0/255.0 blue:67.0/255.0 alpha:1.0]];

[[UIToolbar appearance] setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
3
JulianB

Je sais que c'est vieux, mais cela pourrait être utile pour quelqu'un;

Vous pouvez utiliser une catégorie et y définir * la propriété [translucent][1]

@implementation UINavigationBar (MakeTranslucent)

-(void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];


    self.translucent = NO;
}
@end
  • J'ai utilisé willMoveToWindow, je ne sais pas si c'est une bonne idée donc UAYOR.
2
BDR

Je pense que l'api d'apparence ne prend pas en charge la propriété translucide de la barre de navigation. Mais vous pouvez le faire pour toute l'application comme celle-ci, veuillez consulter ce code -

ici, l'écran de menu est un contrôleur de vue racine.

MenuScreen *ms = [[MenuScreen alloc]initWithNibName:@"MenuScreen" bundle:nil];

UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:ms];

//This will set property for whole App.
[nv.navigationBar setTranslucent:NO];

self.window.rootViewController = nv ;
0
kshitij godara

Voir l'extrait de la documentation du code UIKit:

/*
     New behavior on iOS 7.
     Default is YES.
     You may force an opaque background by setting the property to NO.
     If the navigation bar has a custom background image, the default is inferred 
     from the alpha values of the image—YES if it has any pixel with alpha < 1.0
     If you send setTranslucent:YES to a bar with an opaque custom background image
     it will apply a system opacity less than 1.0 to the image.
     If you send setTranslucent:NO to a bar with a translucent custom background image
     it will provide an opaque background for the image using the bar's barTintColor if defined, or black
     for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
     */

Correct Swift 4 est

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = .white
0
fewlinesofcode