Search code examples
iosuinavigationbarios13

iOS 13 navigation bar appearance setBackIndicatorImage not working


I'm trying to use the new iOS navigation bar appearance API to configure my app's back button indicator, but it isn't working:

    let bar = self.navigationController!.navigationBar
    let sz = CGSize(20,20)
    let arrow = UIImage(systemName:"arrowtriangle.left")!
    let indic =
        UIGraphicsImageRenderer(size:sz).image { ctx in
            arrow.draw(in:CGRect(0,0,20,20)) // indicator is arrow
    }
    let indicmask =
        UIGraphicsImageRenderer(size:sz).image { ctx in
            ctx.fill(CGRect(0,0,20,20)) // mask is entire image
        }
    bar.standardAppearance.setBackIndicatorImage(
        indic, transitionMaskImage: indicmask)

All I'm seeing is a big blue rectangle.

enter image description here

What's going on?


Solution

  • It's a very silly bug: Apple has the parameters backward! Just swap the image to go where the mask should be and the mask to go where the image should be, and all will be well:

        let bar = self.navigationController!.navigationBar
        let sz = CGSize(20,20)
        let arrow = UIImage(systemName:"arrowtriangle.left")!
        let indic =
            UIGraphicsImageRenderer(size:sz).image { ctx in
                arrow.draw(in:CGRect(0,0,20,20)) // indicator is arrow
        }
        let indicmask =
            UIGraphicsImageRenderer(size:sz).image { ctx in
                ctx.fill(CGRect(0,0,20,20)) // mask is entire image
            }
        bar.standardAppearance.setBackIndicatorImage(
            indicmask, transitionMaskImage: indic) // swap!
    

    enter image description here