Cod*_*Guy 3 iphone uiviewcontroller ios
我有一个非常简单的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];
}
Run Code Online (Sandbox Code Playgroud)
基本上,我只是在控制器的顶部有一个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, 0, 640, 48)];
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我以纵向模式启动应用程序,然后我以横向模式进入.基本上,theBar仍然保持正常的大小,并没有调整大小.我确信这是一个愚蠢的问题,但使用旋转功能的正确方法是什么?我希望这样做,如果应用程序以横向模式启动它也可以.在UIViewController首次启动时初始化组件的最佳方法是什么,请记住我希望支持两种方向,同时请记住,我希望能够在整个持续时间内根据方向更改来更改所有内容的大小UIViewController的生命周期?谢谢!
您要做的是更改现有theBar对象的框架,而不是实例化新框架.你可以这样做:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation
duration:(NSTimeInterval)duration {
CGRect f = CGRectMake(0,
0,
CGRectGetWidth(self.view.frame),
CGRectGetHeight(theBar.frame);
theBar.frame = f;
}
Run Code Online (Sandbox Code Playgroud)
请注意,使用的值self.view.frame包含旋转后的值.另请注意,我在这里使用的功能与您的功能不同.我没有使用你正在使用的功能测试它,所以我不能说这是否有效.最后,你可以通过在theBar上设置autoresizingmask来完全避免这种情况viewDidLoad:
[theBar setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21368 次 |
| 最近记录: |