Come rimuovere l'effetto lucido/lucente dai pulsanti sulle barre di navigazione? Se personalizzo la barra di navigazione utilizzando un'immagine personalizzata, i pulsanti non sono interessati, posso rimuovere l'effetto da essi (la linea e il gloss), o definire un codice colore esadecimale per l'intero pulsante, o anche un'immagine personalizzata per loro pure?Rimozione dell'effetto lucentezza dai pulsanti in una UINavigationBar
risposta
Ho appena esaminato il problema. Fondamentalmente, è necessario creare immagini estensibili personalizzate e usarle come sfondo del pulsante per sbarazzarsi della brillantezza. Sostituire i pulsanti posteriori in un UINavigationController è un po 'più difficile. Per questo ho usato un UINavigationControllerDelegate per sostituire il pulsante back predefinito con il mio pulsante personalizzato.
Ecco po 'di codice:
creare una categoria sulla UIBarButtonItem che crea il pulsante personalizzato. Ecco il mio Io uso questa categoria per personalizzare entrambi i pulsanti della barra regolari e pulsanti Indietro:
@interface UIBarButtonItem (UIBarButtonItem_customBackground) + (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector; + (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector; @end @implementation UIBarButtonItem (UIBarButtonItem_customBackground) + (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector { UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom]; [customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f]; customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f]; customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f); customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; customButton.titleEdgeInsets = edgeInsets; UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f]; UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f]; [customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal]; [customButton setTitle:title forState:UIControlStateNormal]; [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted]; [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected]; CGSize size = CGSizeMake(30.0f, 30.0f); if (title != nil) { size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font]; } customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f); customButton.layer.shouldRasterize = YES; customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale]; return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease]; } + (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector { return [self customButtonWithImageNamed:@"navButtonBG.png" selectedImageNamed:@"navButtonPressedBG.png" leftCapWidth:6.0f edgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 5.0f) title:title target:target selector:selector]; } + (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector { return [self customButtonWithImageNamed:@"backButtonBG.png" selectedImageNamed:@"backButtonPressedBG.png" leftCapWidth:12.0f edgeInsets:UIEdgeInsetsMake(0.0f, 11.0f, 0.0f, 5.0f) title:title target:target selector:selector]; } @end
aggiungere il pulsante alla vostra UINavigationBar
UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self selector:@selector(logout)]; self.navigationItem.rightBarButtonItem = logoutButton;
Se anche voi volete sostituire i pulsanti schiena del UINavigationController, di impostazione di un UINavigationControllerDelegate e implementare il metodo willShowViewController in questo modo:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if([navigationController.viewControllers count ] > 1) { UIViewController* backViewController = [navigationController.viewControllers objectAtIndex:(navigationController.viewControllers.count - 2)]; NSString* backText = backViewController.title; UIBarButtonItem* newBackButton = [UIBarButtonItem customBackButtonWithTitle:backText target:navigationController selector:@selector(popViewControllerAnimated:)]; viewController.navigationItem.leftBarButtonItem = newBackButton; viewController.navigationItem.hidesBackButton = YES; } }
Ecco lo stretc immagini Hable che sto usando:
- Pulsante Indietro:
premuto:
- pulsante di listino:
premuto:
- Pulsante Indietro:
YOU. SIAMO. COSÌ. FEAKING. ECCEZIONALE!!!!! Ti amo uomo! – Eugene
@Justin Gallagher Justin grazie grazie grazie! –
Come posso aggiungere questo codice a un progetto per usarlo? –
è necessario utilizzare il pulsante personalizzato con immagini senza alcun effetto lucido sulle immagini con cui è possibile eliminare l'effetto gloos del pulsante dalla barra di navigazione.
Per cambiare il tasto posteriore non è necessario attuare il metodo delegato uinavigationcontroller.
È sufficiente impostare la proprietà hidesBAckButton su YES dopo aver impostato il tasto backbutton desiderato, come spiegato da @Justin Gallacher.
self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBackButtonWithTitle:@"Back" target:self.navigationController selector:@selector(popViewControllerAnimated:)];
self.navigationItem.hidesBackButton = YES;
Questo non è nemmeno necessario, l'impostazione della proprietà leftBarButtonItem sostituirà il pulsante Indietro – Daniel
@Rudy - Se si ritiene che la domanda di essere un duplicato di un altro, si prega di votare per chiuderla come tale, piuttosto che modificare il corpo della domanda. –
Questo è un possibile duplicato di [iOS: Come sostituire la luminosità di sfondo della barra di ricerca/luminosità con un colore] (http://stackoverflow.com/questions/6696200/ios-how-to-replace-search-bar-background- shine-glow-with-one-colour) –