因此,讓我試着解釋發生了什麼。所以,我有這種觀點結構:在子子視圖中獲取視圖的框架
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
}
這還不是全部代碼,但我認爲這是重要的組成部分。
請發佈您的代碼或界面構建器的屏幕截圖。你是否以編程方式佈置你的視圖? – Glenn
@Glenn我以編程方式佈局,我用代碼更新了帖子。 –