Retrieving subViews frames in a storyboard

I just want (for now) to get the dimensions of the view on the view controller instance.

[This is an abbreviation for the simple case where I can find the previous question . I’m trying to understand why the scene subViews in Storyboards do not behave as I expect, namely: for example, XIBs - I just want to get the sizes of my routines before anything is actually drawn on the screen]

To reduce the problem to a new, clean project, I do this:

  • create a new project with one view with the check "Use storyboard"
  • add a single UIView to the default MainStoryboard_iPad.storyboard (and change its background to green to make it easier to see), with the exception of reducing some parameters, this is the only change I make from the default UIView that I drag onto the scene)
  • select the ViewController.h file in the navigator to open it in your frame under the view frame and insert a pair of curly braces under the @interface directive
  • Ctrl-click-and-drag from UIView in the storyboard to ViewController.h and give it the name outlet firstViewFirstSubView

So now we have for ViewController.h :

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UIView *firstViewFirstSubView; } @end 

Then I will add this method to ViewController.m:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"View Controller will appear. firstViewFirstSubView: %@ ", firstViewFirstSubView); NSLog(@"subView dimmensions: %f by %f at %f, %f", firstViewFirstSubView.frame.size.width, firstViewFirstSubView.frame.size.height, firstViewFirstSubView.frame.origin.x, firstViewFirstSubView.frame.origin.y); } 

At this point, I expect that I can get the sizes of the UIView subspecies. I get all 0s though:

 2012-11-15 15:21:00.743 StoryboardViewBounds[11132:c07] View Controller will appear. firstViewFirstSubView: <UIView: 0x9379730; frame = (0 0; 0 0); autoresize = TM+BM; layer = <CALayer: 0x9378e40>> 2012-11-15 15:21:00.744 StoryboardViewBounds[11132:c07] subView dimmensions: 0.000000 by 0.000000 at 0.000000, 0.000000 

What am I doing wrong? It seems like it should be very simple, so I think I need to skip something simple, either by throwing the correct switch in the storyboard editor or implementing the method that is required for the storyboard.

+4
source share
2 answers

These dimensions are calculated and set when the layoutSubviews call is layoutSubviews that occurs after viewWillAppear . After calling layoutSubviews in UIVIew you can get the dimensions.

Look at the implementation of this method in the view controller: viewDidLayoutSubviews . At this point, you can get the sizes of your subzones. Please note that this call has been available since iOS 5.0, but since you are linking to a storyboard, I assume that you are still working on it or higher.

 - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; NSLog(@"View Controller did layout subviews. firstViewFirstSubView: %@ ", firstViewFirstSubView); NSLog(@"subView dimmensions: %f by %f at %f, %f", firstViewFirstSubView.frame.size.width, firstViewFirstSubView.frame.size.height, firstViewFirstSubView.frame.origin.x, firstViewFirstSubView.frame.origin.y); } 
+8
source

There is a related issue that I am forced to turn to, which I hope will save someone else on the debugging day:

I find out that in the storyboard:

  • segue-push does not cause subView to be placed in -viewDidLayoutSubviews (instead, they are laid out at other times before -viewDidAppear ).
    While ...
  • segue-modal and [navController.storyboard presentViewController:] makes the cause of subViews to be placed in -viewDidLayoutSubviews .

The solution is to put [self.mySubView layoutSubviews] in the viewController -viewDidLayoutSubviews method to manually load the subView inside mySubView .

My thing was that I had a special gradient button that incorrectly initialized the visual appearance.
The button was contained in a scrollView containing a CustomView that contained a custom gradient button.
So basically ... a view button inside scrollView .

The application starts with UINavigationController with loading another ViewController1 .
ViewController1 contains a button that, when clicked, launches the segue-push storyboard on ViewController2 .
(this was organized in a storyboard, controlling the drag and drop from a button in ViewController1 to ViewController2 ).

ViewController2 contains scrollView / CustomView / customButton .
In ViewController2 -viewDidLayoutSubviews I initialize customButton , which is a custom Gradient button that has its own .h / .m files.
GradientButton.m has an initLayers method that configures it graphically and requires that the bounds / frame property of the button be initialized.
However ...
in ViewController2 -viewDidLayoutSubviews , self.customButton had a frame of 0,0,0,0.

A few notes:

  • Yes, I call [super viewDidLayoutSubviews] at the beginning of -viewDidLayoutSubviews .
  • The only view that has been outlined in -viewDidLayoutSubviews is self.view ( ViewController2 initial view linked in the Storyboard connections panel). In my case - self.view - scrollView ).

To solve:

  • in ViewController2 , I created a self.bottomView output for a view containing self.customButton .
  • in ViewController2 -viewDidLayoutSubviews , I call [self.bottomView layoutSubviews]
    • This will cause the customButton frame / borders to customButton .
  • Then I call [self.customButton initLayers] , which now correctly initializes my custom gradient button.

A few notes about ViewController2 -viewDidLayoutSubviews: calling [self.view layoutSubviews] trigger a bottomView frame, but NOT a customButton . In terms of the view hierarchy, -layoutSubviews applies only to subView self.view , and not to any subView these subView s.
This is similar to the segue-push storyboard.

The segue-modal storyboard and the software [navController presentViewController] both seem correctly initialized by all levels of the view hierarchy (all " subView of subView s") by the time -viewDidLayoutSubviews .

+8
source

Source: https://habr.com/ru/post/1446336/


All Articles