在Storyboard中获取子视图的帧 [英] Getting subViews' frames in Storyboard

查看:104
本文介绍了在Storyboard中获取子视图的帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想(现在)在视图控制器的实例化中获取子视图的尺寸。

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

[这是一个简单的案例,我可以找到上一个问题。我试图弄清楚为什么故事板中的场景子视图不像我期望的那样表现,也就是说:像XIB一样 - 我只想在实际绘制到屏幕之前获取子视图的尺寸]

[This is a reduction to as simple a case I can find of a previous question. I am trying to figure out why subViews of scenes in Storyboards are not behaving the way I expect, which is to say: like XIBs do - I just want to get dimensions of my subviews before anything is actually drawn to the screen]

要将问题压缩到一个新的,干净的项目,我这样做:

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


  • 选中使用Storyboard创建一个新的单视图项目

  • 将单个UIView添加到默认的现有 MainStoryboard_iPad.storyboard (和将其背景更改为绿色以使其更容易被看到 - 除了缩小尺寸之外,这是我从默认的UIView我拖到场景上的唯一变化)

  • 选项 - 单击导航器中的 ViewController.h 文件将其放在Storyboard框架下面的自己的框架中,并在 @interface 下插入一对括号指令

  • 从故事板中的UIView控制点击并拖动到 ViewContr oller.h 并告诉它命名插座 firstViewFirstSubView

  • create a new, single view project with "using Storyboard" checked
  • add a single UIView to the default existing MainStoryboard_iPad.storyboard (and change its background to green to make it more easily seen - beyond shrinking the dimensions some, this is the only change I make from the default UIView I drag onto the scene)
  • option-click the ViewController.h file in the navigator to bring it up in its own frame underneath the Storyboard frame and insert a pair of braces underneath the @interface directive
  • control-click-and-drag from the UIView in the Storyboard to ViewController.h and tell it to name the outlet firstViewFirstSubView

所以我们现在有 ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UIView *firstViewFirstSubView;
}
@end

然后,我将此方法添加到ViewController。 m:

Then, I add this method to the ViewController.m:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSLog(@"View Controller will appear. 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);
}

此时,我希望能够获得UIView的尺寸子视图。不过我收到全部0:

At this point, I expect to be able to get the dimensions of the UIView subview. 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's dimmensions: 0.000000 by 0.000000 at 0.000000, 0.000000

我做错了什么?看起来这应该是非常简单的,所以我认为我必须遗漏一些简单的东西,无论是在Storyboard编辑器中抛出正确的开关还是实现Storyboard需要的方法。

What am I doing wrong? It seems like this should be very straightforward, so I think I must be missing something simple, whether throwing the right switch in the Storyboard editor or implementing a method that Storyboard needs.

推荐答案

在调用 layoutSubviews 时会计算并设置这些维度,这是在 viewWillAppear之后发生的。在UIVIew上调用 layoutSubviews 后,您可以获得尺寸。

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.

看看在视图控制器中实现此方法: viewDidLayoutSubviews 。此时,您应该能够获得子视图的尺寸。请注意,此调用从iOS 5.0开始可用,但由于您引用了故事板,我认为您无论如何都在使用它。

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);
}

这篇关于在Storyboard中获取子视图的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆