IOS 8 Custom Return Button

I made the application in iOS 7, and when I switched to Xcode 6.1 and iOS 8.1, my custom feedback buttons no longer appeared, and instead they simply displayed the title of the previous view manager - by default.

I use:

self.navigationItem.backBarButtonItem.title = @""; self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor]; 

This does not work anymore, I decided to install delegates ... (in .h and .m respectively)

.h

 <UINavigationControllerDelegate, UINavigationBarDelegate> 

.m

  self.navigationController.delegate = self; 

I donโ€™t know if you do it differently in iOS8, I was looking for boards and could only figure out how to hide the back button. I know that you need to set the button text back in the parent VC in order to cover yourself. I have included identical code in both VCs.

This code works, so I know that I have the ability to communicate with my navigator, so it doesnโ€™t seem like I have a problem ...

 self.navigationItem.title = @"New <type>"; 

thanks

+6
source share
3 answers

Sometimes with barButtonItems you just need to create new ones and not change old ones. I tried your code, and that didn't work either. It worked

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; UIBarButtonItem *item = [[UIBarButtonItem alloc] init]; item.title = @"Title"; self.navigationItem.backBarButtonItem = item; } 

EDIT

This code should be in the previous view controller, and not on the view controller, which has a back button. Ex. If viewController A comes out to viewController B and you want the back button on view controller B to say โ€œBackyโ€ instead of the viewController A header, then you actually put this code in viewController A, not viewController B

EDIT 2

To dynamically change the title of the back button of a view controller, replace backBtn before you click viewController and assign it the corresponding title.

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let barBtnItem = UIBarButtonItem() navigationItem.backBarButtonItem = barBtnItem if segue.identifier == "seg1" { barBtnItem.title = "Hello 1" } else if segue.identifier == "seg2" { barBtnItem.title = "Hello 2" } } 
+7
source

I need four things [Swift]

  • Use my custom button inverse image
  • Do not show text (for example, "back" or "last controller header")
  • The width of the system, for example. all the back buttons look like this.
  • DRY (don't repeat yourself)

Also, since I do it a lot, and it is so strangely complicated, every time I watch it, I created a category.

in AppDelegate.appDidFinishLaunching .... name it:

 UINavigationBar.customBackButtonSetup("iconBackArrow", hideText: true) 

Add this extension to your own file, for example UINavigationBar + CustomBackButton:

 extension UINavigationBar { // call this once at app start class func customBackButtonSetup(backButtonImageName: String, hideText: Bool) { // Custom back button let insets = UIEdgeInsets(top: 0, left: 0, bottom: -2.5, right: 0) // might have to modify this to perfect vertical alignments let backIndicator = UIImage(named: backButtonImageName)!.imageWithRenderingMode(.AlwaysOriginal).imageWithAlignmentRectInsets(insets) UINavigationBar.appearance().backIndicatorImage = backIndicator; UINavigationBar.appearance().backIndicatorTransitionMaskImage = backIndicator if hideText { let barAppearace = UIBarButtonItem.appearance() barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -100), forBarMetrics:UIBarMetrics.Default) } } } 
+3
source

version of iOS 8+ Swift (can be converted to ObjC without much effort). This method retains all the advantages and behavior of the navigation bar associated with the standard return button (click / pop animation, interactive call, etc.)

 // 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) 
+1
source

Source: https://habr.com/ru/post/980271/


All Articles