Custom back indicator image and iOS 11
1) remove PositionAdjustment if have any. such as
bap.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -64), for: .default)
2) check if the previous ViewController in nav stack has a title
iOS 7: Custom Back Indicator Image Position
That happens because you are just changing the image source of the Back Indicator in your UINavigationView, and not the frame as well.
See, when the UINavigationView is created, the Back Indicator's frame is set to hold the size of the default iOS 7 back button image. The default back button image is bigger than yours, and that's why it looks not aligned.
To fix that you have to reset the Back Indicator's Frame to hold the size of your image. Another option is to create a UIButton with the right frame size and image and assign to a UIBarButtonItem. Then you can replace the backBarButtonItem from your UINavigationItem with the new UIBarButtonItem you created.
Swift Custom NavBar Back Button Image and Text
You can do something like that:
let yourBackImage = UIImage(named: "back_button_image")
self.navigationController?.navigationBar.backIndicatorImage = yourBackImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
self.navigationController?.navigationBar.backItem?.title = "Custom"
Your image will only have one color though
Use UINavigationBar appearance back icon for custom button?
UINavigationBar.appearance().backIndicatorImage
is an optional value, therefore you won't be able to get the system default chevron from this. Rather, the system will use the image provided here if not null, otherwise revert to the system default.
If targeting iOS 13+, you can make use of Apple's SF Symbols, in particular the back button icon is referred to as chevron.left
. To use this, call UIImage(systemName: "chevron.left")
. For earlier versions of iOS, you'll have to use an image set asset. You could target all versions of iOS using if #available(iOS 13.0, *) { ... } else { ... }
, where you display the system image if on iOS 13+ for improved UI appearance.
func addBackButton() {
let backButton = UIButton(type: .custom)
if #available(iOS 13.0, *) {
backButton.setImage(UIImage(systemName: "chevron.left"), for: .normal)
}
else {
backButton.setImage(UIImage(named: "backChevon"), for: .normal)
}
backButton.imageView?.contentMode = .scaleAspectFit
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}
Setting a Custom Image for a Back Button on UINavigationBar
try this code
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;
then define goback method like this
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
Default navigation style is misplaced for iOS 11
So I found this answer and I did the same.
Removing this line from AppDelegate
works for me.
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment (UIOffsetMake(0, -60), for: .default)
Related Topics
Prevent Users from Modifying Part of the Text in Slcomposeviewcontroller
iOS Launch Screen in React Native
Why Do I Need @1X, @2X and @3X iOS Images
How to Programmatically Add a Uisegmentedcontrol to a Container View
Avplayer Shows Black Screen When Playing Multiple Videos in Swift3 iOS
Resize Superview After Subviews Change Dynamically Using Autolayout
How to Get the Rootviewcontroller from a Pushed Controller
Uisearchcontroller Persisting After Segue
Setting Multiple Times for Notifications in Swift
Core Text Calculate Letter Frame in iOS
How to Get Email Id of User Using Facebook Sdk 4.7 in iOS 9
Xcode 10B5 - Duplicate Symbol Linker Error, Can't Compile with Crashlytics
Xcode - How to Drag a Component from One View to Another Without Losing Its Frame
Can't Load Uiviewcontroller Xib File in Storyboard in Swift