0

In my iOS app I'm using auto layout to adjust my view's width to its parents by settings it constraint to trailing, leading, top and bottom to 0.

I want to do some calculation based on the width of the view, however I cannot seem to find the right event where the view's new width is calculated from it's layout. It always stuck on 600 including in events like layoutSubviews or updateConstraints.

What is the correct event? What is the correct approach?

Brian
  • 14,610
  • 7
  • 35
  • 43
vondip
  • 13,809
  • 27
  • 100
  • 156

2 Answers2

1

I've done this when one subview causes a sibling subview's frame to change, I add a KVO notification on the layer within the subview. Not sure if it's "correct" but it works.

[self.layer addObserver:self
             forKeyPath:@"bounds"
                options:0
                context:NULL];

...

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary<NSString *,id> *)change
                   context:(void *)context {
    // calculate here.
}
estobbart
  • 1,137
  • 12
  • 28
-1

The place where the proper frame will be available is after viewDidAppear. You can do the calculations in this method.

Sumeet
  • 1,055
  • 8
  • 18