我有一个在 iPad 和 iPhone 之间变化的标题/品牌形象,但在从纵向变为横向时不会改变图片,这真的很重要。
我有这个 HeaderView 类,它由 tableViewControllers 调用,如下所示:
self.tableView.tableHeaderView = [[HeaderView alloc] initWithText:@""];
它拥有一个 UIImageView:
@interface HeaderView : UIImageView{
}
- (id)initWithText:(NSString*)text;
- (void)setText:(NSString*)text;
@end
对于 M 文件,我们找到了我没有得到我想要的结果的地方:
#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;
}
我还添加了这些方法:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
我敢肯定这是一个经典的问题,但我很困惑。另外,如果我删除
[headerImageView setImage:image];
, 和
[self addSubview:headerImageView];
,图像仍然出现,这意味着
if (self = [super initWithImage:image])
正在做所有的展示工作。