3

I'm trying to create a custom layout for some subviews. My file is a ViewController, so I have to refer to its view as self.view. The subviews are subviews of self.view.

When I call [self layoutSubviews]; explicitly in viewDidLoad, it will execute the code in -layoutSubviews without a problem.

However, I've read that I shouldn't call -layoutSubviews explicitly, or call it on self since that's a UIViewController. I tried implementing -setNeedsLayout by calling it like this: [self.view setNeedsLayout] in viewDidLoad, and taking out the explicit call of -layoutSubviews but keeping its declaration. But that didn't seem to work.

I was linked to an article about when layoutSubviews is called and it seems like it should be called when I add subviews, but that doesn't seem to be happening here.

If I want to be safe, and do things the way it suggests in the class reference for UIView and its methods, how should I go about being able to use both methods?

alsuhr
  • 129
  • 2
  • 11

4 Answers4

6

There is no such thing as -[UIViewController layoutSubviews] or -[UIViewController setNeedsLayout]. These are UIView methods.

If you want to use the layoutSubviews system, you need to subclass your top-level UIView and implement it there. By default, it does nothing. If you want to layout your views using the view controller, then I would name the methods something else (like layout, since a view controller doesn't have "subviews") to avoid confusion with UIView.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Rob, I wrote a UISearchBarCustomized class which has layoutSubViews implemented. Then I put a searchbar into a xib file with class name set to UISearchBarCustomized, but the layoutSubViews doesn't get called. Then i also tried to put a UIView into the xib and then put the searchbar onto that UIView...then layoutSubViews didn't get called and also search bar is not shown on that view..do you know how to get it fixed? – trillions Feb 28 '13 at 11:02
  • You should ask this as a question. You should also see http://stackoverflow.com/questions/728372/when-is-layoutsubviews-called. – Rob Napier Feb 28 '13 at 15:27
1

You should never call -layoutSubviews directly. It might be detrimental to performance, but other reasons might as well apply. Instead call -setNeedsLayout. This method sets a flag (or Boolean) that on some next drawing pass the -layoutSubviews method should be called (several methods might set this flag, but it won't force a redraw until the time is right).

Does your -layoutSubviews implementation call -layoutSubviews on the super class?

Perhaps you should show some code so we might help you further with this issue.

Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
1

You can have your layout related codes in - (void)viewWillLayoutSubviews method and call [self.viewControllerObject.view setNeedsLayout] where ever you need to layout your subviews of viewControllerObject.

Krishna Kishore
  • 924
  • 8
  • 4
1

As @Rob Napier answered,

There is no such thing as -[UIViewController layoutSubviews] or -[UIViewController setNeedsLayout]. These are UIView methods.

This is nice, So we are about to subclassing the UIView & assign it as the custom class of the UIViewController's view property. It will work as well, but very lengthy & too much to do for each UIViewController.

There is a easier way (obvious way what we should follow) using the viewWillLayoutSubviews & viewDidLayoutSubviews provided in the UIViewController class.

As the name suggested they are called just before & after, doing the layout thing in the UIView.

Hope these might help Happy coding

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44