iPhone – iOS WillrotateTointerfaceorientation is used correctly

I have a very simple UIViewController and I am trying to figure out how to use willRotateToInterfaceOrientation. My UIViewController has a very simple viewDidLoad method:

< /p>

-(void)viewDidLoad {
[super viewDidLoad];

theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0 f)];
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];
}

Basically, I just have a UINavigationBar at the top of the controller. That’s it. According to what I found on the Internet, I implemented some rotation methods:

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

So, I start the app in portrait mode, and then I enter in landscape mode. Basically, theBar still stays normal size and won't resize. I'm sure this is a stupid question, but use rotation What is the correct way to function? I want to do this, if the app starts it in landscape mode. What is the best way to initialize the component when the UIViewController first starts, remember that I want to support both directions, and also remember that I want to be able to Change the size of all content according to the direction change during the duration of the UIViewController's life cycle? Thanks!

What you need to do is to change the frame of the existing theBar object, not to instantiate a new frame. You can do this:

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

Please note that the value of self.view.frame is used, which contains the rotated value. Also note that the function I am using here is different from your function. I did not use the function you are using to test It, so I can’t say whether this works. Finally, you can avoid this completely by setting the autoresizingmask on theBar in viewDidLoad:

[theBar setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin] ;

I have a very simple UIViewController and I am 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];
}

Basically, I just have a UINavigationBar at the top of the controller. That’s it. According to what I found online, I Some rotation methods are implemented:

- (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 start the application in portrait mode, and then I start in landscape mode Mode is entered. Basically, theBar still remains normal size and will not resize. I am sure this is a stupid question, but what is the correct way to use the rotation function? I want to do this, if the app starts it in landscape mode. What is the best way to initialize the component when the UIViewController first starts, remember that I want to support both directions, and also remember that I want to be able to Change the size of all content according to the direction change during the duration of the UIViewController's life cycle? Thanks!

What you have to do is to change the frame of the existing theBar object instead of instantiating a new frame. You can do this:

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

Please note, use self. The value of view.frame, which contains the rotated value. Please also note that the function I am using here is different from your function. I haven't tested it with the function you are using, so I can't say whether this works. Finally, You can completely avoid this situation by setting the autoresizingmask on theBar in viewDidLoad:

[theBar setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

Leave a Comment

Your email address will not be published.