我有一些旧的 iOS 代码,现在我正在迁移到 sdk7。我必须支持 iOS 5(所以没有自动布局)。代码动态(通过代码)创建 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}
self.view 与导航栏和状态栏重叠,所以这是预期的),但它的大小是{640, 88}
预期的两倍。我也尝试了带有convertPoint:toView
(line 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>>
或者也许有更好的解决方案(没有硬编码)?