ios--解决navigationBar跳转右上角出现的黑色块阴

2019-11-20  本文已影响0人  简鱼7819

问题描述

最近在练习swift时遇到的问题,继承UINavigationController自定义,使用UINavigationController自带的navigationBar做标题栏,push时没问题,pop回来时右上角出现黑色阴影块。

问题效果图gif

问题原因

由于控制器view默认是黑色,push到这个控制器时,而navigationBar默认是透明效果的,因此后面有一个黑色阴影一闪而过。

解决办法

给navigationBar设置BackgroundImage,填充颜色或者真正的图片

1,填充颜色(swift代码示例)

self.navigationBar.setBackgroundImage(imageWithColor(color: UIColor.white), for: UIBarMetrics.default)

private func imageWithColor(color:UIColor) ->UIImage{

        letrect=CGRect(x:0, y:0, width:1, height:1);

        UIGraphicsBeginImageContext(rect.size);

        let context:CGContext = UIGraphicsGetCurrentContext()!;

        context.setFillColor(color.cgColor);

        context.fill(rect);

        let theImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!;

        UIGraphicsEndImageContext();

        return theImage;

 }

2,填充图片,这个贼简单

self.navigationBar.setBackgroundImage(UIImage(named: "nav_bg"), for: UIBarMetrics.default)

遇到解决并记录!!

上一篇下一篇

猜你喜欢

热点阅读