Where to get frame size of custom UIView in its subclass

后端 未结 3 1534
一整个雨季
一整个雨季 2021-02-05 11:24

When does the correct frame size of the UIView is calculated when I add my custom UIView by using storyboard? I can\'t get correct frame size in UIView\'s

3条回答
  •  误落风尘
    2021-02-05 11:49

    func viewWillAppear(_ animated: Bool)
    

    This is called on the viewController after the view hierarchy is in place and the geometry is set up.

    It is usually the best place to arrange geometry-dependent viewController code, and is called just before transition animations are run.

    It is followed by a call to viewDidAppear which is called after transition animations have completed.

    update

    as you want to set this directly in a custom view (rather than in the viewController) the appropriate UIView function to override is layoutSubviews. Be sure to call super.layoutSubviews() in the override.

    override func layoutSubviews() {
        super.layoutSubviews()
        //self.frame will be correct here
    }
    

    This will be called after the viewController's viewWillAppear and before viewDidAppear

    update 2: collectionView cells Collection view cells get their frame data from the collectionview's layout. When a cell needs to know it's geometry, you can override applyLayoutAttributes

    func applyLayoutAttributes(_ layoutAttributes: UICollectionViewLayoutAttributes!)
    

    One of the default layoutAttributes is the frame property. See also my comment below on how to extract the frame directly from collectionView.layout.

提交回复
热议问题