查看iOS的显示问题

时间:2019-02-06 08:38:10

标签: swift setneedsdisplay

我的UI带有自定义标签。

这是最终结果

带标签的表格视图

tableView with tabs

无论何时添加自定义半径和边框,均不会显示该视图。 但是,我可以看到这些选项卡已通过UI检查器添加到stackview中。

我用过:

setNeedsDisplay()
layoutSubviews()
setNeedsLayout()

没有用 有什么想法吗?

  • 这是配置视图和选项卡的方式

协议RoundedTabsViewDelegate:类{     func tabSelected(在索引处:Int)     func roundCorner(已选择:Bool) }

final class RoundedTabsView: UIView {
    init(_ items: [String]) {
        super.init(frame: .zero)
        configure(with: items)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: Properties
    weak var delegate: RoundedTabsViewDelegate?
    private var items = [String]()
    private var itemLabels = [UILabel]()
    private var selectedItemIndex = 0
    private var rect = UIView()

    // MARK: UI
    private let contentStackView = UIStackView()
    private let tabSelectionMarkerView = UIView()

    private func configure(with items: [String]) {
        self.items = items
        configureStackView()
    }

    private func configureStackView() {
        addSubview(contentStackView)
        itemLabels = items.enumerated().map { idx, item in
            let label = UILabel()
            label.text = item
            label.font = UIFont.systemFont(ofSize: 20)
            label.textAlignment = .center
            label.isUserInteractionEnabled = true
            label.tag = idx
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(itemLabelTap))
            label.addGestureRecognizer(tapGesture)
            return label
        }
        contentStackView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        contentStackView.axis = .horizontal
        contentStackView.distribution = .fillEqually
        itemLabels.forEach {
            contentStackView.addArrangedSubview($0)
            self.arrange(subView: itemLabels[selectedItemIndex], selected: selectedItemIndex == $0.tag)
            $0.configure(index: selectedItemIndex)
            $0.setNeedsLayout()
            $0.setNeedsDisplay()
        }


    }

    private func setupSelectionView() {
        addSubview(tabSelectionMarkerView)
        tabSelectionMarkerView.snp.makeConstraints { (make) in
            make.edges.equalTo(itemLabels.first!.snp.edges)
        }
        sendSubview(toBack: tabSelectionMarkerView)
    }

    @objc private func itemLabelTap(_ tapGesture: UITapGestureRecognizer) {
        if let view = tapGesture.view {
            selectedItemIndex = view.tag
            for label in itemLabels {
                label.configure(index: selectedItemIndex)
                self.arrange(subView: label, selected: selectedItemIndex == label.tag)
            }
            delegate?.tabSelected(at: selectedItemIndex)

            //add radius on top left of topView if 1st tab is not selected
            delegate?.roundCorner(selected: view.tag != 0)

        }
    }
}
  • ,这是我初始化自定义视图的viewController:

我在 viewDidAppear

中调用此方法
func setUpHeaderView() {

        //segmented control will always be full width in cases when teh flexible header does not show
        let segmentedWidth: CGFloat = shouldShowFlexibleHeader ? (view.frame.width - margins) / 2 : view.frame.width

        //tableHeaderView
        headerHeight = segmentedHeight + topViewHeight
        headerView.frame = CGRect(x: 0, y: 1, width: view.frame.width, height: headerHeight)
        tableView.tableHeaderView = headerView
        tableView.tableHeaderView?.backgroundColor = Color.gray

        //view
        topView.frame = CGRect(x: 0, y: headerView.frame.height - topViewHeight, width: view.frame.width, height: topViewHeight)
        topView.backgroundColor = .white

        //segmented control - create custom view
        let items = ["Derniers Documents", "Dernières activités"]

        let segmentedControl = RoundedTabsView(items)
        segmentedControl.frame = CGRect(x: 0, y: 01, width: segmentedWidth, height: segmentedHeight)
        headerView.addSubview(topView)
        headerView.addSubview(segmentedControl)
        headerView.bringSubview(toFront: segmentedControl)
        headerView.sendSubview(toBack: topView)

        //delegate
        segmentedControl.delegate = self
        topView.addBordersToTopView()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setupTableView()
        setUpHeaderView()
        if shouldShowFlexibleHeader {
            if !isRegularConfigurationSetup {
                configureFlexibleHeaderController()
            }
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            if !isCompactConfigurationSetup {
                setupCompactConfiguration()
            }
        }
    }

请在此处查看演示

giphy.gif 如您所见,只有当我单击1个标签时,这些标签才会显示。

UITapGestureRecognizer起作用,这意味着视图在这里,但根本不显示。

任何可能导致这种情况的想法?这是调试的挑战

0 个答案:

没有答案