gpt4 book ai didi

objective-c - 'willRotateToInterfaceOrientation' 不适用于我的 DetailViewController

转载 作者:行者123 更新时间:2023-12-01 19:25:53 25 4
gpt4 key购买 nike

我正在 iPad 上开发这个应用程序。我的 MainWindow 包含一个 Split View Controller ,它加载 RootViewController 和 DetailViewController。
我有这张图片位于 DetailViewController 的右下角。
我的应用程序允许不同的方向,因此我希望图像针对不同的方向出现在不同的位置。

这是我在 DetailViewController 中的代码:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f);
}
else if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
}
}

当我启动我的应用程序并且我的 iPad 处于纵向时,图像将正确放置在右下角的 (X:244, Y:518) 处。
但是当我启动我的应用程序并且我的 iPad 处于横向时,图像将不会显示,我怀疑它仍然处于纵向位置。只有当我将我的 iPad 旋转到纵向然后回到横向时,图像才会出现在 (X:183, Y:257) 这是正确的。

我试图做的是将这些代码放在我的 DetailViewController 的 'viewWillAppear' 方法中:
- (void)viewWillAppear:(BOOL)animated 
{
[super viewWillAppear:animated];
if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f);
}
else if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
}
}

但是当我启动我的应用程序时,它仍然有同样的问题。

当我第一次以横向启动我的应用程序时,我该怎么做才能正确放置在 (X:244, Y:518) ?

最佳答案

您可能想尝试将调整大小的逻辑放入 didRotateFromInterfaceOrientation而不是 willRotateToInterfaceOrientation .

我的自定义布局代码仅在我在旋转完成后调用它时才起作用,而不是之前。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
NSLog(@"didRotateFromInterfaceOrientation");

float width;
float height;

if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft))
{
width = [[UIScreen mainScreen] applicationFrame].size.width;
height = [[UIScreen mainScreen] applicationFrame].size.height;
NSLog(@"Changing to portrait: %f x %f", width, height);
}
else
{
width = [[UIScreen mainScreen] applicationFrame].size.height;
height = [[UIScreen mainScreen] applicationFrame].size.width;
NSLog(@"Changing to landscape: %f x %f", width, height);
}

NSLog(@"Changed orientation: %f x %f", width, height);

// Call my custom layout function to setFrame on all my UI controls
[self doLayout:width height:height];

[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}

如果您的应用显示状态栏,那么您可能需要调用 self.view.frame.size.width而不是从 applicationFrame 获取大小.

关于objective-c - 'willRotateToInterfaceOrientation' 不适用于我的 DetailViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com