Search code examples
ipadios6orientation

Specific Orientation Background


Here my code for specifying the background in ipad iOS6,

- (void)viewDidLoad
{
    if ([[UIScreen mainScreen] bounds].size.height == 1024)
    {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"homeipad.png"]];
    }
    else if ([[UIScreen mainScreen] bounds].size.height == 768)
    {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"landscapipad.png"]];
    }
}

its not changing depends on orientation?I have included

-(BOOL)shouldAutorotate
{
     return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

How to allocate specific background image for corresponding orientation in ipad?


Solution

  • Remember viewDidLoad is called only when you make a new UIViewController object in the memory and you push/present that controller object.

    the method name viewDidLoad defines that your viewcontroller view is loaded. Now you can perform the all initialization tasks here.

    Answer to your question.

    The following UIVIewController's delegate methods will help you in this. These methods are called every time when device changes its orientation.

    // Called before interface rotation 
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
    
    // Called after interface rotation
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
    

    One more thing, rather than comparing screen height , you can check the current orientation from the above two methods.

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
    {
        if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
        {
            // Your code goes here for landscape orientation
        }
        else
        {
            // Your code goes here for portrait orientation
        }
    }