Thursday 19 December 2013

Rotate view according to device orientation

This example shows how to handle device orientation and rotate view accordingly to device orientation.


1. Code for .h file.

#import
 


@interface LoginViewController : UIViewController {

    IBOutlet UILabel *lblUserName;
    IBOutlet UILabel *lblEmail;
    IBOutlet UILabel *lblPassword;
    IBOutlet UILabel *lblConfirmPassword;
    IBOutlet UITextField *txtUserName;
    IBOutlet UITextField *txtEmail;
    IBOutlet UITextField *txtPassword;
    IBOutlet UITextField *txtConfirmPassword;
}

@end
2. Code for .m file.

#import "LoginViewController.h"


@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)orientationLandscape
{
    [lblUserName setFrame:CGRectMake(95, 30, 87, 21)];
    [lblEmail setFrame:CGRectMake(95, 69, 111, 21)];
    [lblPassword setFrame:CGRectMake(95, 108, 66, 21)];
    [lblConfirmPassword setFrame:CGRectMake(95, 147, 123, 21)];
    [txtUserName setFrame:CGRectMake(219, 20, 175, 31)];
    [txtEmail setFrame:CGRectMake(219, 59, 175, 31)];
    [txtPassword setFrame:CGRectMake(219, 98, 175, 31)];
    [txtConfirmPassword setFrame:CGRectMake(219, 137, 175, 31)];
}

- (void)orientationPortrait
{
    [lblUserName setFrame:CGRectMake(11, 31, 87, 21)];
    [lblEmail setFrame:CGRectMake(11, 69, 111, 21)];
    [lblPassword setFrame:CGRectMake(11, 108, 66, 21)];
    [lblConfirmPassword setFrame:CGRectMake(11, 147, 123, 21)];
    [txtUserName setFrame:CGRectMake(135, 20, 175, 31)];
    [txtEmail setFrame:CGRectMake(135, 59, 175, 31)];
    [txtPassword setFrame:CGRectMake(135, 98, 175, 31)];
    [txtConfirmPassword setFrame:CGRectMake(135, 137, 175, 31)];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 
{
    // Return YES for supported orientations
    if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
    {
        [self orientationLandscape];
    }
    else
    {
        [self orientationPortrait];
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
  
 
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

No comments:

Post a Comment

Comment