iOS willRotateToInterfaceOrientation proper usage

时光怂恿深爱的人放手 提交于 2019-12-23 18:32:24

问题


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

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

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!


回答1:


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

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


来源:https://stackoverflow.com/questions/6461020/ios-willrotatetointerfaceorientation-proper-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!