iOS willRotateToInterfaceOrientation 正确使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6461020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:30:34  来源:igfitidea点击:

iOS willRotateToInterfaceOrientation proper usage

iphoneiosuiviewcontroller

提问by CodeGuy

I have a very simply UIViewController, and I'm trying to figure out how to use willRotateToInterfaceOrientation. my UIViewController has a very simple viewDidLoad method:

我有一个非常简单的 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];
}

So basically, I just have a UINavigationBar at the top of my controller. That's it. I implemented some methods for rotation, based on what I found online:

所以基本上,我的控制器顶部只有一个 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)];
    }
}

So, I launch the app in portrait mode, and then I twist in in landscape mode. And basically, theBarstill stays it's normal size, and doesn't get resized. I'm sure this is a silly question, but what is the proper way to use the rotation capability? I want to make it so that it also works if the app is launched in landscape mode. What is the best way to initialize my components when the UIViewController first launches, keeping in mind that I want support for both orientations, and also keeping in mind that I want to be able to change the size of everything based on orientation changes throughout the duration of the life of the UIViewController? Thanks!

所以,我在纵向模式下启动应用程序,然后在横向模式下扭动。基本上,theBar仍然保持正常大小,并且不会调整大小。我确定这是一个愚蠢的问题,但是使用旋转功能的正确方法是什么?我想让它在应用程序以横向模式启动时也能正常工作。当 UIViewController 首次启动时初始化我的组件的最佳方法是什么,请记住我希望支持两个方向,并记住我希望能够根据整个持续时间内的方向变化来更改所有内容的大小UIViewController 的生命周期?谢谢!

回答by chris

What you want to do is change the frame of your existing theBar object, and not instantiate a new one. You can do that with something like this:

你想要做的是改变现有的theBar对象的框架,而不是实例化一个新的。你可以这样做:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation 
                                         duration:(NSTimeInterval)duration {
    CGRect f = CGRectMake(0, 
                          0, 
                          CGRectGetWidth(self.view.frame), 
                          CGRectGetHeight(theBar.frame);    
    theBar.frame = f;
}

Note that the value of self.view.frameis used, which contains values post rotation. Also note that the function I'm using here is different than yours. I haven't tested it with the function you're using, so I can't say if that'll work or not. Finally, you can avoid this altogether by just setting the autoresizingmask on theBar in viewDidLoadinstead:

请注意,使用了值self.view.frame,其中包含旋转后的值。另请注意,我在这里使用的功能与您的不同。我还没有用你正在使用的功能测试它,所以我不能说这是否有效。最后,您可以通过在 theBar 上设置 autoresizingmask 来完全避免这种情况viewDidLoad

[theBar setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];