【问题标题】:iOS and corona view landscape issueiOS 和电晕视图横向问题
【发布时间】:2017-02-28 02:18:00
【问题描述】:

我正在尝试使用电晕卡将电晕(游戏平台)与 iOS 项目集成。以下是我想要实现的目标:

  • 以编程方式从 AppDelegate.m 加载 Corona 视图控制器 (ViewController.m/ViewController.h)
  • Corona 视图控制器的强制横向模式

下面是代码:

AppDelegate.m

ViewController *viewController = [[ViewController alloc] init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];

ViewController.m

@interface ViewController ()
  @property (nonatomic, strong) CoronaViewController *coronaController;
@end

- (void)viewDidAppear:(BOOL)animated {
  _coronaController = [[CoronaViewController alloc] init];
  [self addChildViewController:_coronaController];
  
  CoronaView *coronaView = (CoronaView *)_coronaController.view;
  coronaView.frame = self.view.frame;
  
  [self.view addSubview:coronaView];
  [coronaView run];
}

这是它的外观截图:

问题是当我转到横向时,图像应该占据整个屏幕,但它只显示部分图像。

* 更新 * 我能够通过执行以下操作来完成这项工作:

CGRect appFrame = [UIScreen mainScreen].bounds;
_coronaView.frame = CGRectMake(0, -162, appFrame.size.height, appFrame.size.width);
[self.view addSubview:_coronaView];
[_coronaView setNeedsLayout];

但是,我不确定为什么必须将 y 设置为 -162。希望这会有所帮助。

【问题讨论】:

  • 请检查我的回答,如果它解决了您的问题,请告诉我
  • 我试过你的方法,图像看起来很拉伸。截图如下:imgur.com/a/X3IgI
  • 你的imageUIImageView 里面吗?如果是,设置imageView.contentMode = UIViewContentModeScaleAspectFit
  • 图片实际上是从 corona 提供的
  • 图像被拉伸,因为内容模式设置为UIViewContentModeScaleToFill。所以,如果我们无法控制它,我们就无能为力。不全屏问题解决了吗?

标签: ios objective-c iphone coronasdk


【解决方案1】:

这是因为当您从Portrait mode 转到Landscape mode 时,屏幕的宽度和高度会发生变化。所以,coronaView.frame.size.width != [UIScreen mainScreen].bounds.size.widthPortrait 转移到Landscape

要解决这些问题,最好使用AutoLayout设置框架或在ViewControllers中设置框架viewDidLayoutSubviews

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CoronaView *coronaView = (CoronaView *)_coronaController.view;
    coronaView.frame = self.view.frame;
}

- (void)viewDidAppear:(BOOL)animated {
    _coronaController = [[CoronaViewController alloc] init];
    [self addChildViewController:_coronaController];

    CoronaView *coronaView = (CoronaView *)_coronaController.view;
    [self.view addSubview:coronaView];

    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:coronaView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:coronaView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0];
    NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:coronaView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:coronaView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
   [self.view addConstraints:@[constraint1, constraint2, constraint3, constraint4]];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2013-02-23
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 2011-07-01
    相关资源
    最近更新 更多