UINavigationController工具栏和设备方向(旋转)
-
27-09-2019 - |
题
所以我有这样的iPhone应用程序有一个嵌套的设置这样的:[RootViewController]->[UITabBarController]->[UINavigationController]->[subViewControllers].一切都是建立编程方式、未在接口的建设者。
我只是想支持的所有设备的取向。我所有的自动调整的面具的设置以及一切都旋转和扩大/合同是必要的。一切除了NavigationController的导航栏和工具栏。(通常情况下,这些意见的自动调整自动根据不同的方向。即在横向,航栏和工具栏应该自动调整到大约32,和44px在Portrai模式。) 有什么奇怪的是,子视图的导航器是调整,但是实际导航栏和工具栏都没有。换句话说,内容延伸/收缩大约12px或以任何方向的导航栏和工具栏调整。这似乎是自动调整的面具,做他们的工作。
因此,为了试图解决这种情况下,我创造了一个类别UINavigationController应'willRotateToInterfaceOrientation时间:'以触发'sizeToFit'选择。(这种技术来自另一个职位上堆溢出,与一个类似的问题。) 我发现在我的研究,只有最图控制器接收到这个消息,但是,我有我的根视器呼叫这个控制器上的标签,而这又称它在导航控制器,等等。这了照顾这个问题,现在我的套图控制器都被通知时,外部观点是旋转。使用sizeToFit技术,nav条和工具条正在调整自己。然而,这仍然留下这个问题的重新定位的工具栏,以弥补的偏离的尺寸变化。(由于我们的视图坐标在iPhone开始在左上方。)
是什么使这个棘手的做内willRotate方法是,我们没有办法知道有什么新的边界观尺度将是,我们也不知道什么我们当前的方向。(除非你有deviceOrientationNotifications打开,我不要。) 我们所需要的工作是什么新的方向是什么,什么我们的工具栏目前框架的价值观。我能硬代码和计算框架进行调整的古老的方式使用一片划伤的纸张,但是我真的想要保留的动态图大小功能,而不作任何假设有关的设备方面。
因此,使用以下代码,我设法补救这样通过简单的'撞'工具栏12px的相对大小,它就成了。为了防止其过度撞时的用户翻转的设备,使用的当前工具条的框架高度推导出目前的方向。(这意味着我还在制作一个假设有关的工具条之前和之后大小,但是我感觉更好,因为我可以影响到一定程度而不是试图编程方式调整的装置的实物屏幕上。)
@implementation UINavigationController (BLUINavigationControllerAutoRotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return TRUE;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
// Resize the navBar
[[self navigationBar] performSelector:@selector(sizeToFit)
withObject:nil
afterDelay:(0.5f * duration)];
// Read our current frame size
CGRect toolbarFrame = self.toolbar.frame;
// Prevent over adjusting when flipping the device
if ((toolbarFrame.size.height < 38
&& (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight))
|| (toolbarFrame.size.height > 38
&& (toInterfaceOrientation == UIInterfaceOrientationPortrait
|| toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)))
{
// We are already resized and repositioned.
return;
}
// Calculate and apply the toolbar offset
const NSInteger delta = 12; // the difference in size between 44 and 32 px.
toolbarFrame.origin.y += ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
|| (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) ? (delta) : (-delta);
self.toolbar.frame = toolbarFrame;
// Resize the toolbar
[[self toolbar] performSelector:@selector(sizeToFit)
withObject:nil
afterDelay:(0.5f * duration)];
}
@end
所以,现在我已经正确地表现导航栏和工具栏。终端用户永远不会知道,我不得不实施所有这一额外的工作周,以便重新启用什么似乎应该是一个标准的行为。所以我的问题是,为什么我要做这一切?是否有更好的方法我应该知道,或者什么的,我只是我没有做其他地方?(即[navigationController setAutomaticallyResizesToolbarOnrotate:TRUE];或者类似的东西?) 就像我说的,这看来似乎应该是前线,因此对于我们来骗与转发消息和强迫触发的感觉就像一个超黑客设计的,而不是适当的解决方案。
谢谢!
解决方案
你穿上willRotateToInterfaceOrientation消息来签图控制器,从根视器控制-为什么不传递所有其他的旋转的消息,看看是否有帮助?即
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[tabBarController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[tabBarController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
[super didRotateFromInterfaceOrientation:toInterfaceOrientation];
[tabBarController didRotateFromInterfaceOrientation:toInterfaceOrientation];
}
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[tabBarController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
[super didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
[tabBarController didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
}
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateSecondHalfOfRotationFromInterfaceOrientation:toInterfaceOrientation duration:duration];
[tabBarController willAnimateSecondHalfOfRotationFromInterfaceOrientation:toInterfaceOrientation duration:duration];
}
我会有兴趣看看会发生什么,如果你通过所有这些要你的签栏控制器-你的问题似乎喜欢的东西就应当自动发生,而不是所有的额外工作你做了!