I have put a custom image into the backIndicatorImage
using this code:
var backButton = UIImage(named: "locationbar_back")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationController!.navigationBar.backIndicatorImage = backButton
self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = backButton
The image is slightly taller than the standard graphic so I want to be able to vertically center it.
I have tried finding the image in the navbar (not very elegantly) using this code (any better suggestions welcome):
for myview in self.navigationController!.navigationBar.subviews as [UIView]
{
if myview.frame.width == 17 //need a better way of identifying object
{
var newFrame = myview.frame
newFrame.origin.y = 6.0
myview.frame = newFrame
}
}
The frustrating thing is that the image appears momentarily in the right place and then gets put back to its original position.
I have tried calling this in :
with no joy.
Does any one have any suggestions please?
You can change your UIImage position directly by next code. Origin you can set manually:
...
var backButtonImage = UIImage(named: "backButtonImage")!
backButtonImage = changeImagePosition(backButtonImage, origin: CGPointMake(0, 0))
navigationController!.navigationBar.backIndicatorImage = backButtonImage
navigationController!.navigationBar.backIndicatorTransitionMaskImage = backButtonImage
...
func changeImagePosition(image: UIImage, origin: CGPoint) -> UIImage {
let size = image.size
UIGraphicsBeginImageContextWithOptions(size, false, 2)
image.drawInRect(CGRectMake(origin.x, origin.y, size.width, size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With