ios - 自定义导航栏不是矩形

标签 ios swift xcode uinavigationcontroller storyboard

是否可以将导航栏自定义为如下图所示?显然,它具有透明背景而不是灰色背景。

enter image description here

我试过:

UINavigationBar.appearance().setBackgroundImage(UIImage(named:"pattern.png"),
                                                            for: .default)

这似乎捕捉到了颜色,但没有捕捉到导航栏的格式。

enter image description here

我试图避免使用 UIView 或类似的东西创建自定义导航栏,因为这意味着要为这么小的更改做很多额外的工作。我的应用程序有很多使用导航 Controller 的 View 。

提前致谢。

最佳答案

使用它来将锯齿形图案添加到您的导航栏(版本 2),或者您可以创建具有所需图案的自定义 UIImage(版本 1(收藏夹))

版本 1 使用 UIImage 并使用 self.navigationController?.navigationBar.setBackgroundImage 方法

获取图像

import UIKit

extension UIImage {
    
    static func pathZigZagForCGRect(rect: CGRect) ->UIBezierPath
    {
        let width = rect.size.width
        let height = rect.size.height
        
        let zigZagWidth = CGFloat(7)
        let zigZagHeight = CGFloat(5)
        var yInitial = height-zigZagHeight
        
        var zigZagPath = UIBezierPath()
        zigZagPath.move(to: CGPoint(x:0, y:0))
        zigZagPath.addLine(to: CGPoint(x:0, y:yInitial))
        
        var slope = -1
        var x = CGFloat(0)
        var i = 0
        while x < width {
            x = zigZagWidth * CGFloat(i)
            let p = zigZagHeight * CGFloat(slope) - 5
            let y = yInitial + p
            let point = CGPoint(x: x, y: y)
            zigZagPath.addLine(to: point)
            slope = slope*(-1)
            i += 1
        }
        
        zigZagPath.addLine(to: CGPoint(x:width,y: 0))
        zigZagPath.addLine(to: CGPoint(x:0,y: 0))
        zigZagPath.close()
        return zigZagPath
    }
    

    static func zigZagImage(rect: CGRect,color:UIColor)->UIImage {
        
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        var ctx = UIGraphicsGetCurrentContext()!
        ctx.clear(CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.height))
        
        ctx.setFillColor(color.cgColor)
        let path = UIImage.pathZigZagForCGRect(rect: rect)
        ctx.addPath(path.cgPath)
        ctx.fillPath()
        //draw triangle
        
        let img = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        return img
    }
    
}

使用它

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.navigationBar.setBackgroundImage( UIImage.zigZagImage(rect: CGRect(x: 0, y: 0, width: (self.navigationController?.navigationBar.frame.size.width)!, height: (self.navigationController?.navigationBar.frame.height)! + 20),color:UIColor.red).resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
    }

结果

enter image description here

版本 2 使用 layerMask

func pathZigZagForView(givenView: UIView) ->UIBezierPath
    {
        let width = givenView.frame.size.width
        let height = givenView.frame.size.height
        
        let zigZagWidth = CGFloat(7)
        let zigZagHeight = CGFloat(5)
        var yInitial = height-zigZagHeight
        
        var zigZagPath = UIBezierPath()
        zigZagPath.move(to: CGPoint(x:0, y:0))
        zigZagPath.addLine(to: CGPoint(x:0, y:yInitial))
        
        var slope = -1
        var x = CGFloat(0)
        var i = 0
        while x < width {
            x = zigZagWidth * CGFloat(i)
            let p = zigZagHeight * CGFloat(slope) - 5
            let y = yInitial + p
            let point = CGPoint(x: x, y: y)
            zigZagPath.addLine(to: point)
            slope = slope*(-1)
            i += 1
        }
        
        zigZagPath.addLine(to: CGPoint(x:width,y: 0))
        zigZagPath.addLine(to: CGPoint(x:0,y: 0))
        zigZagPath.close()
        return zigZagPath
   }

func applyZigZagEffect(givenView: UIView) {
    let shapeLayer = CAShapeLayer(layer: givenView.layer)
    shapeLayer.path = self.pathZigZagForView(givenView: givenView).cgPath
    shapeLayer.frame = givenView.bounds
    shapeLayer.masksToBounds = true
    givenView.layer.mask = shapeLayer
}

使用它

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.navigationBar.backgroundColor = UIColor.red
        self.applyZigZagEffect(givenView: (self.navigationController?.navigationBar)!)
    }

希望对你有帮助

关于ios - 自定义导航栏不是矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357660/

相关文章:

swift - 将SCNNode或UIView添加到SCNView的中心,以便检测其他SCNNode

ios - 如何在 xcode 中为自定义 TextView 设置文本类型

iphone - iPhone 之间的屏幕共享

ios - 创建一个处理通用类型的函数来访问不同 View 的属性

ios - 可编程显示具有透明背景的 UIViewController

ios - 你如何以一致的速度移动 IOS SKSprite

ios - 如何限制 UICollectionView 的单元格显示数量 - swift

objective-c - 将swift文件导入objective-c项目

ios - 检查 `if let` 是否为零

ios - iPhone UILocalNotification 加载特定 View