0

I've got a header/brand image which changes between iPad and iPhone, but will not change the picture when changing from portrait to landscape, which is really important that it does.

I have this HeaderView Class, which is called by tableViewControllers like this:

self.tableView.tableHeaderView = [[HeaderView alloc] initWithText:@""];

which holds a UIImageView:

@interface HeaderView : UIImageView{

}
- (id)initWithText:(NSString*)text;
- (void)setText:(NSString*)text;


@end

For the M file, we find where I'm not getting the result I want:

#import "HeaderView.h"

#define IDIOM    UI_USER_INTERFACE_IDIOM()
#define IPAD     UIUserInterfaceIdiomPad
@interface HeaderView()
{
    UILabel*label;
}
@end

@implementation HeaderView 

- (id)initWithText:(NSString*)text
{
    UIImage* img = [UIImage imageNamed:@"WashU.png"];
    UIImage* iPhonePortrait = [UIImage imageNamed:@"WashU2.png"];
    UIImage* image = [[UIImage alloc] init];


    UIInterfaceOrientation interfaceOrientation = [UIApplication     sharedApplication].statusBarOrientation;

        //different header image for different devices...
    if(IDIOM==IPAD){
        image = img;
    }
    else{
        if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        image = iPhonePortrait;
        }
        else if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
        {
            iPhonePortrait = nil;
            image = img;
        } 
    }    
    [headerImageView setImage:image];
    if (self = [super initWithImage:image]){

    }

    return self;
}

I also have added these methods:

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

-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

I'm sure its a classic sort of question, but I'm reasonably stumped. Also, if I remove

[headerImageView setImage:image]; 

, and

[self addSubview:headerImageView];

, the image still shows up, which means that

if (self = [super initWithImage:image])

is doing all the display work.

Cristik
  • 30,989
  • 25
  • 91
  • 127
Morkrom
  • 578
  • 7
  • 26
  • If the 'img' and the 'iPhonePortrait' are identical besides a 90 degree rotation, then the rotation might be occurring but you can't tell. That is, you install the 'iPhonePortrait' but the UI rotates it 90 back to 'img' - end result is that it appears nothing happened... Possible solution thus is: don't change 'image' (just like you did for iPad), let the UI do the rotation. – GoZoner Mar 01 '13 at 17:25
  • They are not identical images, but quite different. – Morkrom Mar 01 '13 at 17:38
  • Does the answer from this SO question help you? http://stackoverflow.com/questions/12540152/iphone-ipad-app-orientation – rogueFoo Mar 02 '13 at 06:41

3 Answers3

0

didRotateFromInterfaceOrientation is sent to your view controller after rotation, you can implement that to change your image.

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
      ...change your image here
}
ethyreal
  • 3,659
  • 2
  • 21
  • 25
0

You are not actually assigning the image to the imageView. Add:

headerImageView.image = image;

somewhere after 'image' has been set (based on iPad vs iPhone and Portrait vs Landscape).

Stefan
  • 5,203
  • 8
  • 27
  • 51
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • Thank you for your consideration, though this may have solved part of the problem, we're not cooking with gas yet. – Morkrom Mar 02 '13 at 23:14
  • You are adding the image to both the headerView and 'self' (with initWithImage). I really should only be in one. Also, how do you know that the headerView is actually viewable - you haven't set coordinates, etc. If your coordinates are messed up, then when rotating some stuff might drop off outside of the view. – GoZoner Mar 03 '13 at 01:29
  • You are correct... the subview has nothing to do with displaying the image but this: does... if (self = [super initWithImage:image]) { } – Morkrom Mar 04 '13 at 14:51
0

As your code, you do not need allocate (UIImage*)image property, because you will assign (UIImage*)img or (UIImage*)iPhonePortrait. And should assign image to headerImageView.image.

[headerImageView setImage:image];