【问题标题】:getting subview height to estimate another view height获取子视图高度以估计另一个视图高度
【发布时间】:2021-09-04 03:51:42
【问题描述】:

您好,我想获取子视图的高度来估计 detailUIView heightAnchor。在 detailUIView 中,我有 4 个使用自动约束(以编程方式)布局的标签,我想获取视图高度,以便我可以估计 detailUIView。在函数 estimateCardHeight 中,我正在迭代所有子视图并尝试获取每个子视图的高度并添加它们,但我得到 0.0。我在这里错过了什么,我无法弄清楚。

class ComicDetailViewController: UIViewController {
    var comicNumber = Int()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationItem.title = "Comic Number: \(comicNumber)"
    }
    let detailUIView = DetailComicUIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        // Do any additional setup after loading the view.
        detailUIView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(detailUIView)
        addConstraints()
        print(estimateCardHeight())
    }
   
    public func configure(with viewModel: XKCDTableViewCellVM){
        detailUIView.configure(with: viewModel)
        comicNumber = viewModel.number
        
        
    }
    private func estimateCardHeight() -> CGFloat{
        var allHeight : CGFloat = 0
        for view in detailUIView.subviews{
            allHeight += view.frame.origin.y
        }
       return allHeight
    }
 
    
    private func addConstraints(){
        var constraints = [NSLayoutConstraint]()
        constraints.append(detailUIView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 10))
        constraints.append(detailUIView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 20))
        constraints.append(detailUIView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -20))

        constraints.append(detailUIView.heightAnchor.constraint(equalToConstant: self.view.frame.height/3))
        
        NSLayoutConstraint.activate(constraints)
    }

    

}

【问题讨论】:

    标签: ios swift uiview


    【解决方案1】:

    你不见了view.layoutIfNeeded()

        addConstraints()
        view.layoutIfNeeded()
        print(estimateCardHeight())
    

    作为apple's saying

    使用此方法强制视图立即更新其布局。使用 Auto Layout 时,布局引擎会根据需要更新视图的位置以满足约束的变化。

    如果布局更新未决,则立即布局子视图。


    我猜:

    估计高度的方法,

       private func estimateCardHeight() -> CGFloat{
            var allHeight : CGFloat = 0
            for view in detailUIView.subviews{
                allHeight += view.frame.size.height
                // allHeight += view.frame.origin.y
            }
           return allHeight
        }
    

    【讨论】:

      【解决方案2】:

      实现它的另一种方法。

       override func viewDidLayoutSubviews() {
          super.viewDidLayoutSubviews()
          print(estimateCardHeight())
      }
      

      viewDidLayoutSubviews()

      调用以通知视图控制器它的视图刚刚布置了它的子视图。

      【讨论】:

      • 我是在 viewDidLayoutSubiews 中调用 view.layoutIfNeeded() 还是在 viewDidLoad 中调用
      • 你不需要打电话给view.layoutIfNeeded(),如果你在func viewDidLayoutSubviews()estimateCardHeight()
      • func viewDidLayoutSubviews() 中,没有待处理的布局更新,view.layoutIfNeeded() 退出而不修改布局或调用任何与布局相关的回调。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      相关资源
      最近更新 更多