我正在编写一个iPhone应用程序(与大多数应用程序一样)支持自动旋转:您可以旋转手机,其视图可以旋转并适当调整大小 .
但我正在为 navigationItem.titleView
(导航栏的 Headers 区域)分配一个自定义视图,当手机旋转时,我无法正确调整视图大小 .
我知道你在想什么,“只需将它的 autoresizingMask
设置为 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
”,但它设置了我的视图 autoresizingMask
,然后我的视图没有调整大小;我希望它能够调整大小 .
问题是,如果我设置了 autoresizingMask
,那么只要该视图可见,它就会正确调整大小;但在这种情况下, titleView
的大小变得混乱:
-
运行应用程序,手机处于纵向模式 . 一切都很好看 .
-
做一些导致应用程序将另一个视图推送到导航堆栈的内容 . 例如 . 单击导致调用
[self.navigationController pushViewController:someOtherViewController animated:YES]
的表格行或按钮 . -
查看子控制器时,将手机旋转至横向 .
-
单击"Back"按钮返回顶级视图 . 此时, Headers 视图混乱:尽管您正在横向模式下握住手机,但 Headers 视图的大小仍然像在纵向模式下一样 .
-
最后,将手机旋转回纵向模式 . 现在事情变得更糟了: Headers 视图缩小了尺寸(因为导航栏变小了),但由于它已经太小了,现在它太小了 .
如果您想自己重现,请按照以下步骤操作(这有点工作):
-
使用Xcode的"Navigation-based Application"向导制作应用程序 .
-
进行设置,以便顶级表视图具有行,当您单击它们时,将详细视图推送到导航堆栈 .
-
在顶级视图控制器和详细视图控制器中包含此代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- 仅在顶级视图控制器中包含此代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Create "Back" button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Master"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
// Create title view
UILabel* titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,500,38)] autorelease];
titleView.textAlignment = UITextAlignmentCenter;
titleView.text = @"Watch this title view";
// If I leave the following line turned on, then resizing of the title view
// messes up if I:
//
// 1. Start at the master view (which uses this title view) in portrait
// 2. Navigate to the detail view
// 3. Rotate the phone to landscape
// 4. Navigate back to the master view
// 5. Rotate the phone back to portrait
//
// On the other hand, if I remove the following line, then I get a different
// problem: The title view doesn't resize as I want it to when I:
//
// 1. Start at the master view (which uses this title view) in portrait
// 2. Rotate the phone to landscape
titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.navigationItem.titleView = titleView;
}
- 最后,按照我的重复步骤 .
那么......我做错了什么?有没有办法让我的titleView始终正确调整大小?
7 回答
我有类似的东西 - 但它返回(弹出)到根视图控制器 . 最后,我选择以下内容进行弹出:
它奏效了 . 可能有一种更好的方法,但是 - 在我已经花费在这个问题上的所有时间之后 - 这对我来说已经足够了 .
您还应该设置
UIImageView
的contentMode
以使横向和/或纵向模式正确显示titleView
:imgView.contentMode=UIViewContentModeScaleAspectFit;
整个序列:(自我是
UIViewController
实例)我通过跟踪customView的初始帧来处理同样的问题,然后在UIButton子类的-setLandscape方法中切换它和初始帧的缩放CGRect . 我使用UIButton子类作为navigationItem.titleView和navigationItem.rightBarButtonItem .
在UIButton子类中 -
然后在InterfaceOrientation委托中,我在customViews上调用-setLandscape方法来改变它们的大小 .
在UIViewController中 -
(回答我自己的问题)
我通过手动跟踪titleView的边距(它与导航栏边缘的距离)来实现这一点 - 在视图消失时保存,并在视图重新出现时恢复 .
我们的想法是,我们不会将titleView恢复到之前的确切大小;相反,我们正在恢复它,以便它具有与之前相同的边距 . 这样,如果手机已旋转,则titleView将具有新的适当大小 .
这是我的代码:
在我的视图控制器的.h文件中:
在我的视图中控制器的.m文件:
对于IOS5以后,因为这是一个老问题...这就是我如何完成相同的问题, Headers 文本没有正确对齐 .
在ios5 / 6 sims上测试工作正常 .
这就是我做的:
viewTitle是在xib中创建的视图,它采用navigationBar的大小,在添加后,titleView调整大小以留出空间到后退按钮 . 轮换似乎工作正常 .
我有同样的问题,但我似乎得到了以下代码的解决方法 .
在我的例子中,自定义视图是一个UITextField,但我希望这会对你有所帮助 .