2017-09-23 211 views
-1

因此,让我试着解释发生了什么。所以,我有这种观点结构:在子子视图中获取视图的框架

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 
    } 

这还不是全部代码,但我认为这是重要的组成部分。

+0

请发布您的代码或界面构建器的屏幕截图。你是否以编程方式布置你的视图? – Glenn

+0

@Glenn我以编程方式布局,我用代码更新了帖子。 –

回答

0

最明显的解决方案是获取HeaderView的帧在MenuViewlayoutSubviews函数。

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


更新时间:

正确的解决方案是无效的布局时的集合视图的变化范围。

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

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

,并调用collectionViewinvalidateLayoutMenuViewlayoutSubviews

+0

我想要的大小,因为我有4个单元格,我希望他们的大小是屏幕的1/4。 –

+0

@LeonardoEmilioDominguez检查我更新的答案。 – Bannings

+0

我试过更新的方法,并没有工作。 –

0

这个问题需要更多的上下文,但是您是否想要在代码中获取帧大小,或者您是否声明视图没有按照其约束进行布局。

如果是在代码中,您需要知道frame.size在调用viewDidAppear之前不可用。

+0

我用代码更新了帖子。我想过使用viewDidAppear,但事情是,我从UIView继承和viewDidAppear它是一个viewController方法。 –