我有一个UINavigationController,上面有两个UIViewControllers(A和B).从A,我把B推到堆栈上.然后用户重新定位设备.我需要在屏幕上移动一些东西(按钮等),以便在A上的新方向上可见.
我发现-shouldAutorotateToInterfaceOrientation在A和B上被调用(并返回YES).但是-will/-didRotateFromInterfaceOrientation只能在可见的ViewController(B)上调用.当B从堆栈中弹出时,A以新的(正确的)方向显示,但没有根据需要移动按钮.
为了解决这个问题,我发现自己实现了以下模式:
在头文件中:
@interface A : UIViewController {
// ...
UIInterfaceOrientation layoutOrientation;
}
// ...
- (void)orientationChanged;
@end
Run Code Online (Sandbox Code Playgroud)
在.m文件中:
- (void)viewDidLoad {
// ...
layoutOrientation = self.interfaceOrientation;
}
- (void)viewWillAppear:(BOOL)animated {
// ...
if (layoutOrientation != self.interfaceOrientation) {
[self orientationChanged];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self orientationChanged];
}
- (void)orientationChanged {
// move my buttons
layoutOrientation = self.interfaceOrientation;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我正在检查-viewWillAppear中的方向是否发生了变化,并在需要时进行更新UI的工作.它运行得很好,但这看起来很乏味,而且(b)我的各种类中有很多重复的代码,例如A.我可以通过将代码移动到一个共同的超类来修复(b),但这似乎仍然是我不应该这样做.
有没有更好的方法在导航堆栈上不是最顶层的视图上移动我的按钮?我的观点来自.xibs,如果我需要检查IB中的某些内容.我应该只是设计我的视图,使他们在方向改变时不需要移动按钮吗?
谢谢!
-layoutSubviews我更喜欢在;中进行尽可能多的布局。有多种方法可以检测方向,但我只是将bounds.size.width与bounds.size.height进行比较。
willAnimateRotationToInterfaceOrientation:duration:更适合“特殊”动画,例如将视图从底部滑出并从侧面滑入。
将其与笔尖中的布局结合起来是很乏味的。最近,我刚刚在笔尖中模拟了纵向 UI,直到它运行良好,然后编写了等效的代码来执行相同的操作,但也支持横向。
| 归档时间: |
|
| 查看次数: |
3212 次 |
| 最近记录: |