2017-09-23 6 views
-1

何が起こるか説明しようとします。したがって、私はこのビュー構造を持っています:サブビュー内のビューのフレームを取得する

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

私は各ビュー内で自動レイアウトを使用しています。

私の問題は、フレームサイズを使用していくつかのビューをアンカーするとき、フレームはまだ0.0なので、レイアウトは何もしません。それはViewControllerからまだ得られていない可能性がありますヘッダービュー内で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

もっとも明白な解決策は、のlayoutSubviews funcでHeaderViewのフレームを取得することです。

しかし、なぜあなたはHeaderViewのサイズを取得しようとしていますか?自動レイアウトでは、UIエレメント間の関係を使用してアプリのUIを整理するので、フレームの具体的な値に気を付ける必要はありません。


更新:

適切な解決策は、あなたのレイアウトを無効にされたときにコレクションビューの変更の境界。

shouldInvalidateLayout(forBoundsChange:)を無効にしてtrueを返す必要があります。

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

そしてcollectionView年代MenuViewlayoutSubviewsinvalidateLayoutを呼び出します。

+0

私は4つのセルを持っているのでサイズがほしいですし、そのサイズを画面の1/4にしたいのです。 –

+0

@LeonardoEmilioDominguez最新の回答を確認してください。 – Bannings

+0

更新された方法を試してもうまくいかなかった。 –

0

質問にはもう少しコンテキストが必要ですが、コード内でフレームサイズを取得しようとしているか、ビューが制約に従ってレイアウトされていないと述べていますか?

コード内にある場合はviewDidAppearが呼び出されるまでframe.sizeが利用できないことに注意する必要があります。

+0

私はコードを使って投稿を更新しました。私はviewDidAppearの使用について考えましたが、事実は、私はUIViewとviewDidAppearからサブクラス化しています、それはviewControllerメソッドです。 –

関連する問題