Antoine Antoine - 6 months ago 98
iOS Question

Change backarrow position of UINavigationBar backbutton

Is it possible to adjust the position of the backbutton arrow in a custom UINavigationBar subclass. This is needed as the position is wrong in a navigationbar with a custom adjusted height.

Answer

I finally fixed it as followed, using imageWithAlignmentRectInsets:

 self.backIndicatorImage = UIImage(named: "back_detail")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal).imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, -3, 0))
 self.backIndicatorTransitionMaskImage = UIImage(named: "back_detail")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal).imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, -3, 0))

Edit

Added some more explanation to clarify the implementation:

class VDLNavigationBar: UINavigationBar {

    override func awakeFromNib() {
        // Back button
        self.backIndicatorImage = UIImage(named: "back_detail")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal).imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, -3, 0))
        self.backIndicatorTransitionMaskImage = UIImage(named: "back_detail")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal).imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, -3, 0))    
    }
}

Use this custom subclass in your UINavigationController

Comments