1

I work with iOS 6. I created a custom sub-class of UIView that has my own layout logic described in overridden layoutSubviews method. I expect to support both Portrait and Landscape modes so invocation of layoutSubviews is critical for me. I re-arange and resize sub-views programmatically inside layoutSubviews without using any constraints.

Everything works fine if I operate with simple controls or UIViews added as sub-views into my custom sub-class. But if I add at least one NIB-based sub-view that has Auto Layout with some constraints then layoutSubviews is never called on rotation in my sub-class and therefore I can't update the layout.

This is probably something very simple, but I couldn't find the solution here and since I am new to iOS can't figure by myself. Please help.

  • Are you trying to rearrange the subviews when the device is rotated? If so you should register for device rotation notifications and adjusts your subviews when the rotation is detected. – dana0550 Aug 03 '13 at 06:24
  • Since I work with a sub-class of UIView I can't override `didRotateFromInterfaceOrientation` method, because it's a part of UIViewController class. Anyway, the problem is not with listening of rotation notifications, the problem is that `layoutSubviews` isn't called if there are NIB sub-views with Auto Layout in my custom sub-class. – Eugene Tiutiunnyk Aug 03 '13 at 07:31
  • This worked for me `override func layoutSubviews() { super.layoutSubviews() DispatchQueue.main.async { // you can get updated frame etc } } ` – ChanOnly123 May 03 '19 at 10:41

2 Answers2

4

The issue seemed to be much deeper than I expected. I spent two days doing debugging and reading about all this stuff with mixing Auto Layout and Autoresize layout approaches and eventually I figured that it's really hard to combine some views with Auto Layout and to have some intermediate views with programmatic layout.

I temporary fixed mentioned problem by just manually calling layoutSubviews and setting Frame of my custom sub-class of UIView in the super's layoutSubviews method.

2

Yes I think you have the same issue where the layoutSubviews is not being called altogether. Please check the following links and see :-

When is layoutSubviews called?

UIView layoutSubviews not being called

"Auto Layout still required after executing -layoutSubviews" with UITableViewCell subclass

These questions are similar to your issue . Hope they help.

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
  • 1
    Well, thanks for the links. I've seen some of them already and after reviewing the rest so far I was not able to extract a solution :-/ – Eugene Tiutiunnyk Aug 03 '13 at 06:31