willRotateToInterfaceOrientation not working in Universal MasterDetail app

So I have this code for changing the image on my UIImage view. And I get a very strange behavior both on iPad and iPhone.

This piece code work perfectly fine and gives me the result I want:

- (void)viewWillAppear:(BOOL)animated {
    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeRight||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeLeft) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundLandscape.png"];
    } 
    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortrait||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortraitUpsideDown) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];
    }
}

But the exact same code placed in another method doesn't give me the desired result! It just takes the Portrait picture and uses it for Landscape. And as for Portrait orientation, it uses completely different image!

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)
toInterfaceOrientation duration:
(NSTimeInterval)duration {

    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeRight||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeLeft) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundLandscape.png"];
    } 
    else if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortrait||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortraitUpsideDown) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];
    }

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

When I put a breakpoint for the line self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"]; it fires even if the orientation is landscape!

I don't have much experience with working on Universal apps. I've just taken Apple's Master-Detail App Template and modified it a bit. What can be a problem here? Thanks in advance!

Answers


Use toInterfaceOrientation to determine the orientation in willRotateToInterfaceOrientation method. When this function is called statusBarOrientation is still the old one.

For your information didRotateFromInterfaceOrientation is called after device rotation is changed. If you query statusBarOrientation here it should be correct.


Need Your Help

Remove whitespaces from beginning, between, end

regex postgresql

I have the fallowing string ' this is my string ' is it possible to remove all the white spaces from the beginning and the end and just have one space between words.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.