我有一些旧的iOS代码,现在我正在迁移到sdk7。我必须支持iOS5(所以没有自动布局)。代码动态地(通过代码)创建UI的一些部分。
为了处理导航视图可能与导航栏重叠的可能性,我执行了以下代码:
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect navBarRect = self.navigationController.navigationBar.bounds; // 1
CGPoint bottomLeft = [self.navigationController.navigationBar convertPoint: CGPointMake(0, CGRectGetMaxY(navBarRect))
toView: self.view]; // 2
navBarRect = [self.navigationController.navigationBar convertRect: navBarRect
toView: self.view]; // 3
DDLogVerbose(@"Test positions bottomLeft=%@ navBarRect=%@",
NSStringFromCGPoint(bottomLeft), NSStringFromCGRect(navBarRect));
CGFoat localTop = CGRectMaxY(navBarRect);
CGRect frame = self.view.bounds;
...
这localTop
假设给我正确的y
位置,我可以开始在iOS版本上独立添加我的项目,并且可以抵制任何新的Apple发明。
但这不起作用。标记为1
的行给出了正确的边界({0, 0}, {320, 44})
。但是,在3
左上角执行navBarRect
之后{0, 20}
是{640, 88}
(自我视图与导航栏和状态栏重叠,所以这是预期的),但其大小为convertPoint:toView
这是预期的两倍。
我还尝试使用2
版本(行self.view
),但这给了我相同的结果。
我已经检查了导航的所有超级视图,并且3
直到公共视图,我没有找到任何转换,所以这个框架在行(lldb) po self.view
<UIView: 0x1d9ac1f0; frame = (0 0; 320 411); layer = <CALayer: 0x1d9acae0>>
(lldb) po [self.view superview]
<UIViewControllerWrapperView: 0x1d9b5b20; frame = (0 20; 320 411); layer = <CALayer: 0x1d9b5b80>>
(lldb) po [[self.view superview] superview]
<UINavigationTransitionView: 0x1d9aa040; frame = (0 0; 320 431); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x1cd2a470>>
(lldb) po [[[self.view superview] superview] superview]
<UILayoutContainerView: 0x1cd207c0; frame = (0 0; 320 431); autoresize = W+H; layer = <CALayer: 0x1d975980>>
(lldb) po [self.navigationController navigationBar]
<UINavigationBar: 0x1cd08d60; frame = (0 20; 320 44); clipsToBounds = YES; opaque = NO; autoresize = W; gestureRecognizers = <NSArray: 0x1cd09400>; layer = <CALayer: 0x1cd09070>>
(lldb) po [[self.navigationController navigationBar] superview]
<UILayoutContainerView: 0x1cd207c0; frame = (0 0; 320 431); autoresize = W+H; layer = <CALayer: 0x1d975980>>
之后如何改变大小?< / p>
{{1}}
或许有更好的解决方案(没有硬编码)?