sri
sri

Reputation: 3389

How to change view of the viewcontroller when change the orientation in iPhone?

I'm developing an iPhone application which supports 2 orientations. I have 2 UIView files for this view controller. I need to set corresponding UIView file to the view controller according to the device interface orientation. Can you guide me on how to change the orientation?

Upvotes: 0

Views: 650

Answers (3)

Saranya
Saranya

Reputation: 1521

just give this delegate in your controller for both orientations.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

Upvotes: 2

jigneshbrahmkhatri
jigneshbrahmkhatri

Reputation: 3637

Why do you want to use two views for both orientation? You can set autoresizemask properties of controls, if portrait and landscape both mode has similar control to be show at same place. Otherwise, you need to use two views in

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

or you can have notification of UIDevice

 UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification;

- (void)beginGeneratingDeviceOrientationNotifications;      // nestable
- (void)endGeneratingDeviceOrientationNotifications;

Upvotes: 3

Luke
Luke

Reputation: 11476

Assuming you have set the -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation to return your supported orientations, you can use the following methods which can all be set in your UIViewController to check for orientation changes and then set your views:

// called just before the user interface starts to rotate - my advice would be to use this method to save a copy of the current orientation.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

// called after the interface has finished rotating. Here, you can compare the old orientation with the new one - if you went from portrait to landscape, then update your views accordingly.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

Hope this helps!

Upvotes: 3

Related Questions