2

I have set the UINavigation bar appearance which looks like follwing,

Code:

fileprivate class func barButtonAppearance() {
    var attributes = [String : AnyObject]()
    attributes[NSFontAttributeName] = UIFont(name: .Regular, size: 14)
    attributes[NSForegroundColorAttributeName] = UIColor.descriptionColor()
    UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: UIControlState())

    let backImage =  UIImage.image(assetID: .NavigationBarBack, caps: UIEdgeInsetsMake(0, 23, 0, 0)).withRenderingMode(.alwaysTemplate)
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImage, for: .normal, barMetrics: .default)

    UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
}

It was working fine until we tested our application in iOS 11.

If I comment the below code

let backImage =  UIImage.image(assetID: .NavigationBarBack, caps: UIEdgeInsetsMake(0, 23, 0, 0)).withRenderingMode(.alwaysTemplate)
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImage, for: .normal, barMetrics: .default)

It works fine but with the default apple given back button.

Here is the screen shot of the navigation bar appearing,

enter image description here

I was not able to get what was happening. Could someone please suggest me the workaround? Thanks.

nayem
  • 7,285
  • 1
  • 33
  • 51
Shreesha Kedlaya
  • 340
  • 4
  • 19

2 Answers2

2
UINavigationBar.appearance().backIndicatorImage = image.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image.withRenderingMode(.alwaysOriginal)

if #available(iOS 11, *) {
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .highlighted)
} else {
    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -60, vertical: -60), for: .default)
}

image is UIImage. no need to create base controller or write code on every controller. Just put these lines in app delegate.

Prabhjot Singh Gogana
  • 1,408
  • 1
  • 14
  • 39
0

You can use this method:

func addBackButton() {
    let leftButton = UIBarButtonItem(image: Asset.backIco, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.popViewControllerAnimated(_:)))
    self.navigationItem.leftBarButtonItem = leftButton
}

Paste it in extension UIViewController

Daniel Kuta
  • 1,634
  • 15
  • 24