ios - View Controller 的 View 属性

标签 ios view uiviewcontroller frame bounds

我正在开发专用计算器的界面,并且在 IB 中进行了以下设置: Interface Builder

我使用了 IB 中的“Container View”并将其设置为等于我的自定义 View Controller “ButtonViewController”。

然后,在我的 ButtonViewController.m 文件中,我有以下内容:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.0 blue: 0.0 alpha: 1.0 ];
    NSLog(@"self.view.bounds.size.width:\t%f", self.view.bounds.size.width);
    NSLog(@"self.view.bounds.size.height:\t%f", self.view.bounds.size.height);

    NSLog(@"self.view.frame.size.width:\t%f", self.view.frame.size.width);
    NSLog(@"self.view.frame.size.height:\t%f", self.view.frame.size.height);
}

我得到以下屏幕输出:

Screenshot

以及以下控制台输出:

self.view.bounds.size.width:    320.000000
self.view.bounds.size.height:   460.000000
self.view.frame.size.width:     320.000000
self.view.frame.size.height:    460.000000

Interface Builder 指出“容器”宽 280 像素,高 364 像素,因此由于某种原因,我没有获得正确的值。

我什至试图找到 superboundsframe 尺寸,但它们仍然是 320x460。

有人可以告诉我我做错了什么吗?

提前致谢

[更新]

仅为其他人提供一些信息: 我已经使用类似的 UI 控件进行了一些测试,以下是我的发现: (cvObject 是一个 UICollectionView 对象)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

-(void)viewWillLayoutSubviews
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

-(void)viewDidLayoutSubviews
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

我得到的是:

-[FPViewController viewDidLoad]: self.cvObject.bounds:  {{0, 0}, {0, 0}}
-[FPViewController viewDidLoad]: self.cvObject.frame:   {{0, 0}, {0, 0}}
-[FPViewController viewWillAppear:]: self.cvObject.bounds:  {{0, 0}, {0, 0}}
-[FPViewController viewWillAppear:]: self.cvObject.frame:   {{0, 0}, {0, 0}}
-[FPViewController viewWillLayoutSubviews]: self.cvObject.bounds:   {{0, 0}, {0, 0}}
-[FPViewController viewWillLayoutSubviews]: self.cvObject.frame:    {{0, 0}, {0, 0}}
-[FPViewController viewDidLayoutSubviews]: self.cvObject.bounds:    {{0, 0}, {280, 312}}
-[FPViewController viewDidLayoutSubviews]: self.cvObject.frame: {{20, 128}, {280, 312}}
-[FPViewController viewDidAppear:]: self.cvObject.bounds:   {{0, 0}, {280, 312}}
-[FPViewController viewDidAppear:]: self.cvObject.frame:    {{20, 128}, {280, 312}}

因此您可以看到,只有在 View 布置其 subview 之后,它才确定对象的框架和边界。

最佳答案

您应该在 viewDidAppear 中重新检查这些值,您会发现在那里看到正确的值:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.0 blue: 0.0 alpha: 1.0 ];

    // frame and bounds are not entirely reliable at this point

    NSLog(@"%s: self.view.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.bounds));
    NSLog(@"%s: self.view.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.frame));
}

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

    // frame and bounds have generally been updated correctly by this point

    NSLog(@"%s: self.view.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.bounds));
    NSLog(@"%s: self.view.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.frame));
}

关于ios - View Controller 的 View 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16009243/

相关文章:

android - 应用程序如何只供用户读取并且只有管理员有权发布(使用 firestorm 数据服务器)

ios - UIbutton 在自定义 UITableViewCell 中释放

javascript - 如何在razor javascript函数中写入 "@"?

postgresql - 在 PostgreSQL View 中自动重新制定条件

ios - 如何在 crittercism 中使用崩溃日志找到崩溃?

iphone - 如何切割json字段值并将其存储在不同的索引中

Android 将 View 添加到锁定屏幕

ios - 源 View Controller 与呈现 View Controller

ios - 点击后隐藏 View

objective-c - UIViewController purgeMemoryForReason : Crashing on iOS 5