/ / Impostazione di un'immagine personalizzata per un pulsante Indietro su UINavigationBar - iphone, goal-c, xcode, uinavigationcontroller, uibutton

Impostazione di un'immagine personalizzata per un pulsante Indietro su UINavigationBar - iphone, goal-c, xcode, uinavigationcontroller, uibutton

Sto cercando di impostare un'immagine personalizzata per il pulsante Indietro che viene automaticamente posizionata su una barra di navigazione quando una nuova vista viene inserita nello stack.

Ho provato ad aggiungere quanto segue nel viewDidLoad del viewController genitore:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Ho anche provato il seguente:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BackButton.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = btn;

L'utilizzo di UIAppearance produce risultati piuttosto strani: L'immagine BackButton sembra essere posizionata sotto il titolo originale

risposte:

35 per risposta № 1

prova questo codice

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"]  ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;

quindi definire il metodo goback in questo modo

- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}

26 per risposta № 2

Da iOS 7.0 è disponibile un nuovo metodo nell'API backIndicatorImage. Puoi usarlo al posto di setBackButtonBackgroundImage se non desideri che l'immagine venga allungata per adattarla al testo (ad esempio, se desideri una freccia indietro personalizzata di dimensioni fisse). Ecco un esempio rapido:

let image = UIImage(named: "back_button")

UINavigationBar.appearance().backIndicatorImage = image
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image

Puoi nascondere il testo dal pulsante usando questo trucco:

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -66), for: .default)

19 per risposta № 3

Questo è il codice che sto utilizzando e funziona perfettamente nella mia app iOS 5. Questo codice proviene da application:didFinishLaunchingWithOptions: nel delegato dell'app:

UIImage * backButtonImage = [UIImage imageNamed: @"back-button-image"];
backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage: backButtonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];

Potrebbe essere necessario utilizzare un'immagine estensibile, in cui il "punto" rivolto a sinistra è il tappo sinistro


o in Swift

let backButtonImage = UIImage(named: "back-button-image")
backButtonImage = backButtonImage?.stretchableImageWithLeftCapWidth(15, topCapHeight: 30)
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, forState: .Normal, barMetrics: .Default)

4 per risposta № 4

iOS 8+ versione Swift (può essere convertito in ObjCcon poco sforzo). Questo metodo preserva tutti i vantaggi e il comportamento della barra di navigazione relativa al pulsante Indietro standard (animazione su push / popping, gesto pop interattivo, ecc.)

// Call the code ONE TIME somewhere on app launch to setup custom back button appearance
UINavigationBar.appearance().tintColor = UIColor.blackColor()
// If you need custom positioning for your back button, in this example button will be 1 px up compared to default one
// Also only vertical positioning works, for horizontal add offsets directly to the image
let backImageInsets = UIEdgeInsetsMake(0, 0, -1, 0)
// Get image, change of rendering (so it preserves offsets made in file), applying offsets
let backImage = UIImage(named: "YourButtonImageAssetFileName")?.imageWithRenderingMode(.AlwaysOriginal).imageWithAlignmentRectInsets(backImageInsets)
// Setting images
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage

// Call EACH TIME BEFORE pushing view controller if you don"t need title near your back button arrow
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
// Push controller
self.navigationController?.pushViewController(vc, animated: true)

2 per risposta № 5

Prova il codice qui sotto, funziona per me:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];

0 per risposta № 6

Questo ha funzionato per me:

let button1 = UIBarButtonItem(
image: #imageLiteral(resourceName: "back_arrow"),
style: .plain,
target: self,
action: #selector(self.backBtnTapped(_:))
)
self.navigationItem.leftBarButtonItem  = button1

0 per risposta № 7

Questo codice ha funzionato per Swift 4 e XCode 9.4 :

Aggiungi questo codice a AppDelegate "s func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool da applicare a tutti i controller di visualizzazione:

//Custom back button
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
let backImg: UIImage = UIImage(named: "back")!
barButtonItemAppearance.setBackButtonBackgroundImage(backImg, for: .normal, barMetrics: .default)

let image = UIImage()

UINavigationBar.appearance().backIndicatorImage = image
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image