如何解决iOS willRotateToInterfaceOrientation正确用法
| 我有一个非常简单的UIViewController,我正在尝试弄清楚如何使用willRotateToInterfaceOrientation。我的UIViewController有一个非常简单的viewDidLoad方法:-(void)viewDidLoad {
[super viewDidLoad];
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,48.0f)];
theBar.tintColor = [UIColor orangeColor];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@\"The Title\"];
item.hidesBackButton = YES;
[theBar pushNavigationItem:item animated:YES];
[item release];
[self.view addSubview:theBar];
}
因此,基本上,我只是在控制器顶部有一个UINavigationBar。而已。我根据网上发现的内容实施了一些轮换方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,640,48)];
}
}
因此,我以纵向模式启动该应用程序,然后以横向模式启动该应用程序。基本上,ѭ2仍然保持其正常大小,并且不会调整大小。我确定这是一个愚蠢的问题,但是使用旋转功能的正确方法是什么?我要制作它,以便在横向模式下启动应用程序时也可以使用。首次启动UIViewController时,初始化组件的最佳方法是什么,请记住我要同时支持两个方向,还要记住我希望能够根据整个持续时间的方向变化来更改所有内容的大小UIViewController的寿命?谢谢!
解决方法
您要做的是更改现有theBar对象的框架,而不是实例化一个新的框架。您可以使用以下方法做到这一点:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation
duration:(NSTimeInterval)duration {
CGRect f = CGRectMake(0,CGRectGetWidth(self.view.frame),CGRectGetHeight(theBar.frame);
theBar.frame = f;
}
注意,使用了“ 4”的值,其中包含旋转后的值。另外请注意,我在这里使用的功能与您使用的功能不同。我尚未使用您正在使用的功能对其进行过测试,因此无法确定该功能是否有效。最后,您可以通过在Bar5 the中的Bar上设置autoresizingmask来完全避免这种情况:
[theBar setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。