quanguyen
quanguyen

Reputation: 1463

Swift - UIView when done updating its frame?

I have a UITableViewCell in which I add my custom view, say

class MyView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    convenience required init(size: CGFloat) {
        self.init(frame: CGRect.zero)

        height = size

        // init my other sub contents [1]
    }
}

From my UITableViewCell:

let myView = MyView(size: 35)
contentView.addSubView(myView)

My problem: my other UI components in MyView depend on the width and height of MyView and it's too early to set their position in [1] because MyView's frame is now (x: 0, y: 0, width: 0, height: height)

Where should I put my other UI components in MyView? Is there a delegate in UIView that tells me: "Hey, the frame of the view is updated with its actual size."? (like viewDidAppear in a UIViewController)

Regards,

Upvotes: 0

Views: 51

Answers (1)

Oskar
Oskar

Reputation: 3702

You can do

override var frame: NSRect {
    didSet {
        //update your subviews
    }
}

In your MyView class. But really, you should just look into setting up proper constraints:

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/

Upvotes: 1

Related Questions