UINavigationItem在UINavigationBar的后面

问题描述 投票:0回答:1

我已经将UINavigationBar设置为带有阴影的圆角,但是它删除了我的UINavigationItem。我尝试以编程方式将其设置回去,但将其设置在顶部栏的后面。

    class RoundedShadowCorners {
    func shadowTopBar(_ topBar: UINavigationBar,_ offset: CGFloat,_ navigationItem: UINavigationItem){
        topBar.isTranslucent = false
        topBar.tintColor = UIColor.orange

        topBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        topBar.shadowImage = UIImage()
        topBar.backgroundColor = UIColor.white
        let shadowView = UIView(frame: CGRect(x: 0, y: -offset, width: (topBar.bounds.width), height: (topBar.bounds.height) + offset))
        shadowView.backgroundColor = UIColor.white
        topBar.insertSubview(shadowView, at: 1)

        let shadowLayer = CAShapeLayer()
        shadowLayer.path = UIBezierPath(roundedRect: shadowView.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath

        shadowLayer.fillColor = UIColor.white.cgColor

        shadowLayer.shadowColor = UIColor.darkGray.cgColor
        shadowLayer.shadowPath = shadowLayer.path
        shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
        shadowLayer.shadowOpacity = 0.8
        shadowLayer.shadowRadius = 2

        shadowView.layer.insertSublayer(shadowLayer, at: 0)

        topBar.prefersLargeTitles = true
        topBar.topItem?.title = "HJFSKDJKA"
    }
}

偏移量是view.safeAreaInsets.top!如所附图片所示,标题位于图层的后面。

Text is behind

As you can see, layer works

ios swift xcode uinavigationbar
1个回答
0
投票

您可以尝试以下代码:

topBar.bringSubview(toFront: topBar.topItem?.titleView)

设置标题后添加此代码行。

© www.soinside.com 2019 - 2024. All rights reserved.