Getting subViews' frames in Storyboard

前端 未结 2 1305
北恋
北恋 2021-01-03 05:35

I just want (for now) to get the dimensions of a subview on the view controller\'s instantiation.

[This is a reduction to as simple a case I can find of a previous

2条回答
  •  被撕碎了的回忆
    2021-01-03 06:00

    Those dimensions are calculated and set when the call to layoutSubviews is made, which occurs after viewWillAppear. After layoutSubviews is called on the UIVIew you can get the dimensions.

    Look at implementing this method in your view controller: viewDidLayoutSubviews. At this point you should be able to get dimensions of your subviews. Note that this call is available starting in iOS 5.0 but since you reference storyboards I assume you are working at or above that anyway.

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

提交回复
热议问题