我建议做你说过不会做的第一件事:
我想为纵向/横向模式使用特定的 xib 文件,而不是
使用代码以不同方式放置对象
我花了很多时间尝试将不同的 XIB 用于不同的方向,但这是交易:
1) 如果你有一个纵向的 UITextField,当你旋转到横向时,你必须确保纵向模式下的文本在横向模式下是相同的。您必须对每个对象都这样做。
2) 即使您不看第 1) 点,我也无法成功实施这两个 xibs 解决方案。我不是说你做不到,我只是根据我的经验说,我做不到。
所以,如果你至少认为做 1 xib 解决方案,我可以解释一下:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)orientation{
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(@"My view will rotate");
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
[self viewsPositionPortrait];
} else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
[self viewsPositionLandScape];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSLog(@"Register should rotate");
return YES;
}
-(void)viewsPositionPortrait{
//为您的“oneOfmyViews”创建框架以进行纵向旋转
oneOfMyviews.frame=CGRectMake(...,....,...,...);
}
-(void)buttonsPositionLandScape{
//为您的“oneOfmyViews”创建框架以进行横向旋转
oneOfMyviews.frame=CGRectMake(...,....,...,...);
}
然后您可以在 viewsPosition 方法中定义视图的框架。你赢了什么?
1) 干净的代码。
2) 漂亮的过渡。
3) 轻松指定滚动视图的内容大小。
对于您的 UIViews => CustomViewController => UIScrollView 的旋转问题。
你可以这样做:
1) 在 CustomViewController 中的 UIView 中,在自定义视图(上述方法)中执行完全相同的操作。
2) 在具有您的 CustomViewControllers 的视图中,您可以执行以下操作:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
//(everything that i posted above)
[myCustomViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
这样,您将使 customViewController 中的 UIViews 旋转。最后一个提示:禁用 autoresize-subviews 并从你的 UIViews 中取出每个自动尺寸(因为你将单独定义它们的框架)。