使用 Snapkit 的自定义视图在使用时不显示

Posted

技术标签:

【中文标题】使用 Snapkit 的自定义视图在使用时不显示【英文标题】:Custom View using Snapkit not showing when used 【发布时间】:2019-07-10 04:32:01 【问题描述】:

我在swift中设计了一个视图,如下所示。

import UIKit
import SnapKit

@IBDesignable class FlightStopsView: UIView 

    var startPoint: UILabel!
    var endPoint: UILabel!
    var line: UIView!
    var stopsStack: UIStackView!
    var stopCount: UILabel!

    var stops: [Stop]! 
        didSet 
            addStops()
            setNeedsLayout()
            setNeedsDisplay()
        
    

    @IBInspectable var lineColor: UIColor! 
        didSet
            line?.backgroundColor = UIColor.lightGray
            layoutIfNeeded()
        
    

    override init(frame: CGRect) 
        super.init(frame: frame)
        setUpView()

    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)
    

    func setUpView() 

        startPoint = UILabel()
        startPoint.attributedText = "o"
        self.addSubview(startPoint)
        startPoint.snp.makeConstraints  make in
            make.width.equalTo(10)
            make.height.equalTo(10)
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        

        endPoint = UILabel()
        endPoint.attributedText = "o"
        self.addSubview(endPoint)
        endPoint.snp.makeConstraints  make in
            make.width.equalTo(10)
            make.height.equalTo(10)
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
        

        line = UIView()
        line.backgroundColor = UIColor.lightGray
        self.addSubview(line)
        line.snp.makeConstraints  make in
            make.height.equalTo(1)
            make.centerY.equalToSuperview()
            make.leading.equalTo(startPoint.snp.trailing).offset(-1)
            make.trailing.equalTo(endPoint.snp.leading).offset(1)
        

        stopCount = UILabel()
        stopCount.textColor = UIColor.lightGray
        stopCount.font = UIFont.regularFont(Dimens.FontSize.SMALL)
        stopCount.adjustsFontSizeToFitWidth = true
        stopCount.text = "Non Stop"
        self.addSubview(stopCount)
        stopCount.snp.makeConstraints  make in
            make.centerX.equalToSuperview()
            make.top.equalTo(line.snp.bottom).offset(4)
            make.bottom.equalToSuperview()
        

        stopsStack = UIStackView()
        stopsStack.backgroundColor = .clear
        stopsStack.alignment = .center
        stopsStack.distribution = .fill
        stopsStack.contentMode = .center
        stopsStack.spacing = 30
        self.addSubview(stopsStack)
        stopsStack.snp.makeConstraints make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(line.snp.bottom).offset(4.5)
        

        addStops()
    

    func addStops() 
        guard stopCount != nil && stopsStack != nil else 
            return
        

        guard stops != nil else 
            stopCount.text = "Non stop"
            return
        
        stopCount.text = "\(stops.count) stops"

        for stop in stops 
            let stackChild = UIView()
            let stackChildLabel = UILabel()
            let stackChildPointer = UILabel()

            stackChildLabel.text = stop.stopName
            stackChildLabel.textAlignment = .center
            stackChildLabel.textColor = UIColor.lightGray
            stackChildLabel.font = UIFont.regularFont(Dimens.FontSize.SMALL)
            stackChildLabel.adjustsFontSizeToFitWidth = true
            stackChildLabel.numberOfLines = 2
            stackChildPointer.attributedText = “o”

            stackChild.addSubview(stackChildLabel)
            stackChild.addSubview(stackChildPointer)

            stackChild.snp.makeConstraints  make in
                make.width.equalTo(24)
                make.height.equalToSuperview()
            

            stackChildPointer.snp.makeConstraints  make in
                make.width.equalTo(10)
                make.height.equalTo(10)
                make.bottom.equalToSuperview()
                make.centerX.equalToSuperview()
            

            stackChildLabel.snp.makeConstraints make in
                make.width.equalTo(20)
                make.top.equalToSuperview()
                make.bottom.equalTo(stackChildPointer.snp.top)
                make.centerX.equalToSuperview()
            

            stopsStack.addArrangedSubview(stackChild)
        
    

视图的主要目的是显示两个旅行点之间的停靠点。即,如果使用公共汽车作为交通工具,从目的地到来源有多少站。但是,在模拟器上运行时,此视图不会显示在视图中。如果有人能在这里指出我做错了什么,我会很高兴。

【问题讨论】:

【参考方案1】:

问题是您没有将stackChild 设置为stopStacks,另一件事是初始化,这在init?(coder aDecoder: NSCoder) 方法下是必需的,因为从UIStoryboard 添加UIView 时不会init(frame: CGRect)调用。

你可以在下面找到他们修改过的代码:

import UIKit
import SnapKit

@IBDesignable class FlightStopsView: UIView 

    var startPoint: UILabel!
    var endPoint: UILabel!
    var line: UIView!
    var stopsStack: UIStackView!
    var stopCount: UILabel!

    var stops: [Stop]! 
        didSet 
            addStops()
            setNeedsLayout()
            setNeedsDisplay()
        
    

    @IBInspectable var lineColor: UIColor! 
        didSet
            line?.backgroundColor = UIColor.lightGray
            layoutIfNeeded()
        
    

    override init(frame: CGRect) 
        super.init(frame: frame)
        setUpView()
    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)
        //Here is the change: Make sure you setupview in case of you are calling it from Storyboard
        setUpView()
    

    func setUpView() 

        startPoint = UILabel()
        startPoint.text = "0"
        self.addSubview(startPoint)
        startPoint.snp.makeConstraints  make in
            make.width.equalTo(10)
            make.height.equalTo(10)
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        

        endPoint = UILabel()
        endPoint.text = "0"
        self.addSubview(endPoint)
        endPoint.snp.makeConstraints  make in
            make.width.equalTo(10)
            make.height.equalTo(10)
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
        

        line = UIView()
        line.backgroundColor = UIColor.lightGray
        self.addSubview(line)
        line.snp.makeConstraints  make in
            make.height.equalTo(1)
            make.centerY.equalToSuperview()
            make.leading.equalTo(startPoint.snp.trailing).offset(-1)
            make.trailing.equalTo(endPoint.snp.leading).offset(1)
        

        stopCount = UILabel()
        stopCount.textColor = UIColor.lightGray
        stopCount.font = UIFont.regularFont(Dimens.FontSize.SMALL)
        stopCount.adjustsFontSizeToFitWidth = true
        stopCount.text = "Non Stop"
        self.addSubview(stopCount)
        stopCount.snp.makeConstraints  make in
            make.centerX.equalToSuperview()
            make.top.equalTo(line.snp.bottom).offset(4)
            make.bottom.equalToSuperview()
        

        stopsStack = UIStackView()
        stopsStack.backgroundColor = .clear
        stopsStack.alignment = .center
        stopsStack.distribution = .fill
        stopsStack.contentMode = .center
        stopsStack.spacing = 30
        self.addSubview(stopsStack)
        stopsStack.snp.makeConstraints make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(line.snp.bottom).offset(4.5)
        

        addStops()
    

    func addStops() 
        guard stopCount != nil && stopsStack != nil else 
            return
        

        guard stops != nil else 
            stopCount.text = "Non stop"
            return
        
        stopCount.text = "\(stops.count) stops"

        for stop in stops 
            let stackChild = UIView()
            let stackChildLabel = UILabel()
            let stackChildPointer = UILabel()

            stackChildLabel.text = stop.name
            stackChildLabel.textAlignment = .center
            stackChildLabel.textColor = UIColor.lightGray
            stackChildLabel.font = UIFont.regularFont(Dimens.FontSize.SMALL)
            stackChildLabel.adjustsFontSizeToFitWidth = true
            stackChildLabel.numberOfLines = 2
            stackChildPointer.text = "0"

            stackChild.addSubview(stackChildLabel)
            stackChild.addSubview(stackChildPointer)

            //Here is the change: Although you haven't added your `stackChild` to your main `stopsStack`
            stopsStack.addSubview(stackChild)

            stackChild.snp.makeConstraints  make in
                make.width.equalTo(24)
                make.height.equalToSuperview()
            

            stackChildPointer.snp.makeConstraints  make in
                make.width.equalTo(10)
                make.height.equalTo(10)
                make.bottom.equalToSuperview()
                make.centerX.equalToSuperview()
            

            stackChildLabel.snp.makeConstraints make in
                make.width.equalTo(20)
                make.top.equalToSuperview()
                make.bottom.equalTo(stackChildPointer.snp.top)
                make.centerX.equalToSuperview()
            

            stopsStack.addArrangedSubview(stackChild)
        
    

如果这有帮助,请告诉我!

【讨论】:

以上是关于使用 Snapkit 的自定义视图在使用时不显示的主要内容,如果未能解决你的问题,请参考以下文章

发送 PHP 标头时不显示通过 Htaccess 的自定义 405 错误消息

如何在 Android 的自定义视图中显示 ImageView

PagerSlidingTabStrip- 使用列表片段的自定义视图显示突然的行为

按钮未显示在android的自定义列表视图中

Snapkit 和 TableView 中的多个自定义单元格

插入时不调用 Core Data 的自定义验证方法