使用xib自定义标签栏

时间:2018-10-03 10:10:47

标签: ios uitabbarcontroller uitabbar

我使用xib制作自定义标签栏,我需要知道如何将标签栏底部提取到情节提要中,并且需要知道如何为所有视图自动调整该笔尖视图的大小

截屏:

My current tabbar look here

1 个答案:

答案 0 :(得分:0)

定义一个名为MyTabBar的UIView类,并使用相同的类代替要在其中添加标签栏的UIView

class MyTabBar: NibView {

}

现在,创建一个名为NibView的超类,它将处理有关XIB文件合并的所有事情

class NibView: UIView {
    var view: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)        
        xibSetup()
    }
}

extension NibView {

    func xibSetup() {
        view = loadNib()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.frame = bounds
        addSubview(view)

        // add Constraints
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[childView]|", options: [], metrics: nil, views: ["childView": view]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[childView]|", options: [], metrics: nil, views: ["childView": view]))
    }
}

extension UIView {
    func loadNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nibName = type(of: self).description().components(separatedBy: ".").last!
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiate(withOwner: self, options: nil).first as! UIView
    }
}

可以在此处找到有关如何使用笔尖的更多详细信息 https://medium.com/swift2go/swift-custom-uiview-with-xib-file-211bb8bbd6eb