iOS 13导航栏外观setBackIndicatorImage不起作用

时间:2019-09-30 15:45:56

标签: ios uinavigationbar ios13

我正在尝试使用新的iOS导航栏外观API配置我的应用程序的后退按钮指示器,但是它不起作用:

    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)

我所看到的只是一个大的蓝色矩形。

enter image description here

这是怎么回事?

1 个答案:

答案 0 :(得分:2)

这是一个非常愚蠢的错误:Apple的参数向后!只需将图像交换到应放置蒙版的位置,然后将蒙版移至应放置图像的位置,一切都会很好:

    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