我正在研究专用计算器的界面,我在IB中进行了以下设置:
我使用IB中的“容器视图”并将其设置为我的自定义视图控制器“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);
}
我得到以下屏幕输出:
以下控制台输出:
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声明“容器”宽280px,高364px,因此出于某种原因,我没有得到正确的值。
我甚至试图找到super
的{{1}}和bounds
尺寸,但它们仍然是320x460。
有人可以告诉我我做错了吗?
提前致谢
[更新]
其他人的一些信息:
我已经使用类似的UI控件进行了一些测试,这是我发现的:
(frame
是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));
}
所以你可以看到它只是在视图布局其子视图后才确定你的对象的框架和边界。
答案 0 :(得分:2)
您应该在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));
}