【发布时间】:2013-03-01 17:11:33
【问题描述】:
我有一个标题/品牌图像,它在 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])
正在做所有的显示工作。
【问题讨论】:
-
如果 'img' 和 'iPhonePortrait' 除了 90 度旋转之外是相同的,那么旋转可能正在发生,但您无法判断。也就是说,您安装了“iPhonePortrait”,但用户界面将其旋转 90 度回到“img” - 最终结果是它似乎没有发生任何事情......因此可能的解决方案是:不要更改“图像”(就像你为iPad),让 UI 进行旋转。
-
它们不是完全相同的图像,而是完全不同的。
-
这个 SO 问题的答案对您有帮助吗? stackoverflow.com/questions/12540152/…
标签: ios objective-c uiinterfaceorientation