2017-09-23 227 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方法。 –