自定义标头UIView到所有VC?

我正在尝试为项目中的所有 ViewControllers 创建自定义头文件。下图是实际设计。

自定义标头UIView到所有VC?

因此,我尝试使用Xib文件创建自定义视图,如下所示:

import UIKit

class HamburgrView: UIView {
    override init(frame: CGRect) {
        // 1. setup any properties here
        // 2. call super.init(frame:)
        super.init(frame: frame)
        // 3. Setup view from .xib file
        xibSetup()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }
    override open func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        xibSetup()
    }
    override func layoutSubviews() {

    }
    func xibSetup() {

       let  tempView = loadViewFromNib()
        // use bounds not frame or it'll be offset
        tempView.frame = bounds
        // Make the view stretch with containing view
        tempView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        // Adding custom subview on top of our view (over any custom drawing > see note below)
        addSubview(tempView)

    }

    func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of:self))
        let nib = UINib(nibName: "HamburgrView",bundle: bundle)

        // Assumes UIView is top level and only object in CustomView.xib file
        let view = nib.instantiate(withOwner: self,options: nil)[0] as! UIView
        return view
    }


}

我们不知道如何从 SafeArealayoutGuide 绘制带有曲线及其起始位置的汉堡菜单图标。

我尝试使用x位置为负-40的一个视图,并且使cornerRadius不能像上面的图像那样变得完美。

输出效果不好。

自定义标头UIView到所有VC?

我的 ViewController 代码:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let hamView = HamburgrView(frame: .zero)
        self.view.addSubview(hamView)
        hamView.translatesAutoresizingMaskintoConstraints = false
        NSLayoutConstraint.activate([
            hamView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor,constant: 0),hamView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor,hamView.widthAnchor.constraint(equalToConstant: 120),hamView.heightAnchor.constraint(equalToConstant:  80)
            ])
    }

作为@sandip答案,我已经进行了如下更改:

func xibSetup() {

       let  tempView = loadViewFromNib()
        // use bounds not frame or it'll be offset
        tempView.frame = bounds
        // Make the view stretch with containing view
        tempView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        // Adding custom subview on top of our view (over any custom drawing > see note below)
        addSubview(tempView)
        let layer = CAShapelayer()
        layer.path = UIBezierPath.init(arcCenter: CGPoint(x: 0,y: self.center.y),radius: 50,startAngle: -1.5708,endAngle: 1.5708,clockwise: true).cgPath
        layer.fillColor = UIColor.red.cgColor
        self.layer.addSublayer(layer)
    }

输出看起来如下:

自定义标头UIView到所有VC?

任何想法都将不胜感激。

ou562970340 回答:自定义标头UIView到所有VC?

enter image description here

这是您想要的吗?

您可以使用CAShapeLayerUIBezierPath

轻松实现它
    let layer = CAShapeLayer()
    layer.path = UIBezierPath.init(arcCenter: CGPoint(x: 0,y: self.view.center.y),radius: 30,startAngle: -1.5708,endAngle: 1.5708,clockwise: true).cgPath
    layer.fillColor = UIColor.red.cgColor
    view.layer.addSublayer(layer)

在您的代码中,将圆弧中心的x更改为零,将y的位置更改为汉堡包视图的垂直中心,然后将其添加到self.view

希望有帮助

编辑1:

OP在评论更新答案时要求提供完整的代码

class HamburgrView: UIView {
    override init(frame: CGRect) {
        // 1. setup any properties here
        // 2. call super.init(frame:)
        super.init(frame: frame)
        // 3. Setup view from .xib file
        xibSetup()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }
    override open func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        xibSetup()
    }
    override func layoutSubviews() {

    }
    func xibSetup() {

       let  tempView = loadViewFromNib()
        // use bounds not frame or it'll be offset
        tempView.frame = bounds
        // Make the view stretch with containing view
        tempView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        // Adding custom subview on top of our view (over any custom drawing > see note below)
        addSubview(tempView)

    }

    func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of:self))
        let nib = UINib(nibName: "HamburgrView",bundle: bundle)

        // Assumes UIView is top level and only object in CustomView.xib file
        let view = nib.instantiate(withOwner: self,options: nil)[0] as! UIView
        return view
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        let layer = CAShapeLayer()
        layer.path = UIBezierPath.init(arcCenter: CGPoint(x: 0,y: 40),radius: 50,clockwise: true).cgPath
        layer.fillColor = UIColor.red.cgColor
        self.layer.addSublayer(layer)
    }
}

编辑2:

原因OP弧线超出了汉堡包的视野,原因是弧线中心错误

CGPoint(x: 0,y: 40)更改为self.view.center.y应该可以解决此问题。更新了代码以反映相同的内容

为什么要40?

因为OP将汉堡包视图的高度设置为80。所以80/2 = 40

我们为什么不能使用bounds.size.height / 2self.view.center.y

因为在didMoveToSuperview中,OP的约束尚未生效,所以它采用xib中的高度视图,我认为它不是80,因此它将弧中心设置为该值,但是约束将高度更改为80。

这是最终的O / P:

enter image description here

本文链接:https://www.f2er.com/3154672.html

大家都在问