这更是一个普遍的问题,为人们提供指导我,基本上林学习的iPad / iPhone开发和终于遇到了多方位的支持问题。

我抬头DOCO相当数量的,我的书“从iPhone 3发展”上有一个很好的篇章。

但我的问题是这样的,如果我是通过编程修改我的控制(或者甚至可以使用每个方向不同的看法)如何在地球上的人保持自己的代码库?我可以想像的细码/千“如果”检查所有的地方,这将推动我坚果做一个小改动,UI安排这么多的问题。

有没有人有经验处理这个问题?什么是控制它的好办法?

非常感谢 标记

有帮助吗?

解决方案

我这样做,在我的视图控制器两种简单的方法:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self adjustViewsForOrientation:toInterfaceOrientation];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        titleImageView.center = CGPointMake(235.0f, 42.0f);
        subtitleImageView.center = CGPointMake(355.0f, 70.0f);
        ...
    }
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        titleImageView.center = CGPointMake(160.0f, 52.0f);
        subtitleImageView.center = CGPointMake(275.0f, 80.0f);
        ...
    }
}

要保持这种干净的,你可以很容易地划分视图调整/重装/等。与来自单一if-else条件内调用的方法。

其他提示

这要看它是什么你铺设。

如果你看看苹果的设置应用程序,你可以看到,他们使用表格视图的布局,定制细胞最行。有了这一点,你可以让一个简单的界面,通过只填充单元格的宽度相当便宜旋转。这甚至适用于像邮件,事情那里有每行中编辑文本的细胞。和表格可以很容易地全透明的,只有按钮或标签可见,这样他们就不会像表。

您可以得到很多milage的每UIView的的autoresizingMask的。如果有,可以有灵活的高度,一个或多个项目,那么你通常可以得到一个接口的布局,看起来不错的任何一个方向。根据它的外观,有时你可以只脚都在上面。

在极少数情况下,如果所有的界面元素适合在广场,你可以在适当位置旋转它们。

有两次,当你必须明确地处理方向变化。一个是当一个视图移动从旁边到低于另一个上转动。另一种是,当你有不同的图像对于每个方位,例如,如果你总是希望是全宽度。

有有时方法来解决这两个。您可以使用拉伸图片或限制自己每行一个视图。或者你可以锁定方向肯定意见。

如果必须更改的视图的布局,存在显式layoutSubviews方法。你应该尝试处理所有你在这一个方法,有条件的布局。当视图边界发生变化,例如在旋转,或者如果你已经取得的余地键盘它只调用。为每个视图层次自定义视图,需要旋转响应,并从那里布局子视图。

在iPhone SDK的周围有一个MVC架构,如果你把所有的逻辑(模型)从你的UI(视图)分开建造,因此在理论上,那么你将只需要对UI担心在一个地方:您的视图控制器。对于那些,可以有用于每个取向,其中每个随后将只是与一个被加载的if / else选择哪个视图控制器负载的单独视图控制器。

同样的想法适用于iPhone / iPad支持,其中可以加载它可以处理更大的显示器另一个视图控制器。

我不能保证该代码,并在所有诚实上述willRotateToInterfaceOrientation工作很大。下面是另一个对其采取与FBDialog.m来自Facebook的iPhone / iPad的。 (虽然,我认为这是对一个网页视图)

,这里的要点

[[NSNotificationCenter defaultCenter] addObserver:self
  selector:@selector(deviceOrientationDidChange:)
  name:@"UIDeviceOrientationDidChangeNotification" object:nil];


- (void)deviceOrientationDidChange:(void*)object {
  UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  if ([self shouldRotateToOrientation:orientation]) {


    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    [self sizeToFitOrientation:YES];
    [UIView commitAnimations];
  }
}


-(CGAffineTransform)transformForOrientation {
  UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  if (orientation == UIInterfaceOrientationLandscapeLeft) {
    return CGAffineTransformMakeRotation(M_PI*1.5);
  } else if (orientation == UIInterfaceOrientationLandscapeRight) {
    return CGAffineTransformMakeRotation(M_PI/2);
  } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
    return CGAffineTransformMakeRotation(-M_PI);
  } else {
    return CGAffineTransformIdentity;
  }
}

- (void)sizeToFitOrientation:(BOOL)transform {
  if (transform) {
    self.transform = CGAffineTransformIdentity;
  }

  CGRect frame = [UIScreen mainScreen].applicationFrame;
  CGPoint center = CGPointMake(
    frame.origin.x + ceil(frame.size.width/2),
    frame.origin.y + ceil(frame.size.height/2));

  CGFloat scale_factor = 1.0f;
  if (FBIsDeviceIPad()) {
    // On the iPad the dialog's dimensions should only be 60% of the screen's
    scale_factor = 0.6f;
  }

  CGFloat width = floor(scale_factor * frame.size.width) - kPadding * 2;
  CGFloat height = floor(scale_factor * frame.size.height) - kPadding * 2;

  _orientation = [UIApplication sharedApplication].statusBarOrientation;
  if (UIInterfaceOrientationIsLandscape(_orientation)) {
    self.frame = CGRectMake(kPadding, kPadding, height, width);
  } else {
    self.frame = CGRectMake(kPadding, kPadding, width, height);
  }
  self.center = center;

  if (transform) {
    self.transform = [self transformForOrientation];
  }
}

在你的问题,你写道:

  

我可以想像的细码这么多的问题/千“如果”检查所有的地方,这将推动我坚果做一个小的变化到UI布局。

躲闪一种方法是使分裂的从一开始就iPhone / iPad的具体变化的处理视图层次。你只要到视图最初为每个设备加载集。然后创建一个视图控制器就像你通常做,但你也继承您创建的视图 - 控制。一个亚类为每个设备。这就是你可以把设备特定代码,例如定向处理。像这样:

MyViewController.h            // Code that is used on both devices
    MyViewController_iPhone.h // iPhone specific code, like orientation handling
    MyViewController_iPad.h   // iPad specific code, like orientation handling

如果您有兴趣在此方法中,我建议你读的这篇文章。它说明了它在一个非常好的方式。

一的文章提到的事,是这样的:

<强> - 报价的开始 -

这种模式的好处是,我们不必垃圾我们的代码与垃圾,看起来像这样:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // The device is an iPad running iPhone 3.2 or later.
    // set up the iPad-specific view
} else {
    // The device is an iPhone or iPod touch.
    // set up the iPhone/iPod Touch view
}

<强> ---报价的端 -

我希望有所帮助。祝你好运!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top