【问题标题】:UIInterfaceOrientation remains in portraitUIInterfaceOrientation 保持纵向
【发布时间】: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


【解决方案1】:

didRotateFromInterfaceOrientation 在旋转后被发送到你的视图控制器,你可以实现它来改变你的图像。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
      ...change your image here
}

【讨论】:

    【解决方案2】:

    作为您的代码,您不需要分配 (UIImage*)image 属性,因为您将分配 (UIImage*)img 或 (UIImage*)iPhonePortrait。并且应该将图像分配给 headerImageView.image。

    [headerImageView setImage:image];
    

    【讨论】:

      【解决方案3】:

      您实际上并未将图像分配给 imageView。添加:

      headerImageView.image = image;
      

      设置“图像”之后的某个位置(基于 iPad 与 iPhone 以及纵向与横向)。

      【讨论】:

      • 感谢您的考虑,虽然这可能已经解决了部分问题,但我们还没有用煤气做饭。
      • 您正在将图像添加到 headerView 和“self”(使用 initWithImage)。我真的应该只在一个。另外,你怎么知道 headerView 实际上是可见的——你没有设置坐标等。如果你的坐标搞砸了,那么在旋转时,一些东西可能会掉到视图之外。
      • 你是对的...子视图与显示图像无关,但是:确实... if (self = [super initWithImage:image]) { }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多