在子观察中获取视图框架



所以,让我尝试解释会发生什么。因此,我有此视图结构:

ViewController
HeaderView -> Subview of ViewController
MenuView -> Subview of HeaderView

我在每个视图内使用自动布局。

我的问题是,在Menuview中,当我尝试使用框架大小来锚定一些视图时,它仍然是0.0,因此,它没有布局任何内容,我认为这可能是因为它仍然没有从ViewController那里得到它,因为当我在Headerview内部布局Menuview时,我还使用框架尺寸。

有帮助吗?

编辑:顺便说一句,边界也为0.0

代码:

HeaderController

lazy var headerView: HeaderView = {
       let hv = HeaderView(maxHeight: self.maxHeight, medHeight: self.medHeight, minHeight: self.minHeight, paddingBetween: 10)
        return hv
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        tableView.delegate = self
        tableView.dataSource = self
        headerView.headerControllerDelegate = self
    }
    func setupViews() {
        addSubview(collectionView)
        addSubview(lineView)
        bringSubview(toFront: lineView)
        _ = collectionView.anchor(top: topAnchor, bottom: bottomAnchor, right: rightAnchor, left: leftAnchor)
        lineViewLeftAnchor = lineView.anchor(top: nil, bottom: bottomAnchor, right: nil, left: leftAnchor, topConstant: 0, bottomConstant: 0, rightConstant: 0, leftConstant: 0, widthConstant: frame.size.width / 4, heightConstant: 1.5)[1]

    }
}

Headerview

 let menuView: MenuView = {
        let mv = MenuView()
        return mv
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = Theme.PRIMARY_DARK_COLOR
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    convenience init(maxHeight: CGFloat, medHeight: CGFloat, minHeight: CGFloat, paddingBetween containerPadding: CGFloat) {
        self.init(frame: CGRect.zero)
        self.maxHeight = maxHeight
        self.medHeight = medHeight
        self.minHeight = minHeight
        self.containerPadding = containerPadding
        collapseBtn.addTarget(self, action: #selector(handleCollapse), for: .touchUpInside)
        setupViews()
    }
    func setupViews() {
        addSubview(menuView)
        _ = menuView.anchor(top: menuContainer.topAnchor, bottom: menuContainer.bottomAnchor, right: menuContainer.rightAnchor, left: menuContainer.leftAnchor)
    }

menuview

extension MenuView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? MenuCell {
            cell.menuLabel.text = menuItems[indexPath.row]
            cell.menuLabel.textColor = fontColor
            return cell
        }
        return UICollectionViewCell()
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return menuItems.count
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: frame.size.width / 4, height: frame.size.height)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

这不是整个代码,但我认为这是重要的部分。

最明显的解决方案是在MenuViewlayoutSubviews func中获取HeaderView的框架。

但是为什么要获得HeaderView的大小?在自动布局中,您使用UI元素之间的关系安排应用程序的UI,因此您不应该关心帧的具体值。


更新:

当收集视图的边界变化时,适当的解决方案使您的布局无效。

您必须覆盖shouldInvalidateLayout(forBoundsChange:)并返回true

https://developer.apple.com/documentation/uikit/uicollectionviewlayout/1617781-shouldinvalidatatelayoutforboundsc

并在MenuViewlayoutSubviews中调用collectionViewinvalidateLayout

这个问题需要更多的上下文,但是您是否试图抓住代码中的框架大小,还是指出视图并没有按照其约束来布置。

如果是代码中的,则需要意识到frame.size在调用viewDidAppear之前才可用。

最新更新