为以编程方式加载的子视图提供自动旋转支持 - iPad
我有一个 iPad 项目,结构如下: - 应用程序委托 - 主窗口 - 视图控制器 -- View
View Controllers .m 文件以编程方式加载另一个视图并将其放置在屏幕上。该视图将滑入和滑出。
我这样做:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 0, 0);
CalculatorView *v = [[[CalculatorView alloc]
initWithFrame:viewRect] autorelease];
[self.view.window addSubview:v];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
v.view.frame = CGRectMake(0, 0, 460, 320);
[UIView commitAnimations];
}
我遇到的问题是我在此处添加的子视图似乎没有正确的方向。该项目仅支持横向,并启动横向。容器视图很好,它包含一些也很好的按钮。然而,这个以编程方式加载的视图卡在纵向模式下。我提供了以下自动旋转代码(在加载视图的 .m 中):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
但它永远不会被调用。
那么,如何才能以编程方式添加的子视图以横向模式而不是纵向模式加载呢? 蒂亚!
I have an iPad project structured as:
- AppDelegate
- MainWindow
- View Controller
-- View
The View Controllers .m file loads another view programmatically and positions it on the screen. This view is going to be slid in and out.
I do this:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 0, 0);
CalculatorView *v = [[[CalculatorView alloc]
initWithFrame:viewRect] autorelease];
[self.view.window addSubview:v];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
v.view.frame = CGRectMake(0, 0, 460, 320);
[UIView commitAnimations];
}
The issue that I am having is that the subview I'm adding here doesnt' seem to have the correct orientation. The project supports ONLY landscape, and launches to landscape. The container view is fine, and it contains some buttons which are fine as well. However this programmatically loaded view is stuck in Portrait mode. I have provided the following auto-rotation code (In the loaded view's .m):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
But it never gets called.
So, how can I get the programmatically added subview to load in landscape and NOT portrait mode?
TIA!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIView 类不接收方向更改消息。
(特别是
shouldAutorotateToInterfaceOrientation
方法,它是一个 UIViewController 方法)您必须在视图中手动添加一个方法来通知它方向已更改,并且您应该在控制器
shouldAutorotateToInterfaceOrientation
方法中调用此方法。为此,您必须在控制器中创建对视图的引用并自行管理内存。
编辑:如果您的 CalculatorView 很简单,并且您需要的只是在设备旋转后正确更改其框架,我认为最好的方法是使用视图的 autoresizingMask 类似于以下内容
The UIView class does not receive orientation change messages.
(specially the
shouldAutorotateToInterfaceOrientation
method, which is a UIViewController Method)You will have to manually add a method in your view to inform it that the orientation has changed and you should call this method in your controller
shouldAutorotateToInterfaceOrientation
method.To do that you will have to create a reference to you view in your controller and take care of the memory yourself.
EDIT: If you CalculatorView is simple and all you need is to change its frame properly after the device rotation, I think the best approach would be using you view's autoresizingMask similar to the following