iOS willRotateToInterfaceOrientation 正确使用 [英] iOS willRotateToInterfaceOrientation proper usage

查看:34
本文介绍了iOS willRotateToInterfaceOrientation 正确使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的 UIViewController,我正在尝试弄清楚如何使用 willRotateToInterfaceOrientation.我的 UIViewController 有一个非常简单的 viewDidLoad 方法:

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:

-(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];
}

所以基本上,我的控制器顶部只有一个 UINavigationBar.就是这样.根据我在网上找到的内容,我实现了一些轮换方法:

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:

- (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)];
    }
}

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

So, I launch the app in portrait mode, and then I twist in in landscape mode. And basically, theBar still 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 对象的框架,而不是实例化新的框架.你可以这样做:

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:

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

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

Note that the value of self.view.frame is 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 viewDidLoad instead:

[theBar setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

这篇关于iOS willRotateToInterfaceOrientation 正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆