ios ipad 方向

orientation in ios ipad

我有一个 Ipad 应用程序支持 "landscape" 模式,现在我希望它支持这两种模式 "landscape and portrait" 这个项目是用 "xib" 文件制作的

我使用了一些代表,但我没有得到我必须做的解决方案 我使用这个代码

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    //The device has already rotated, that's why this method is being called.
    UIInterfaceOrientation toOrientation = [[UIDevice currentDevice] orientation];
    //fixes orientation mismatch (between UIDeviceOrientation and UIInterfaceOrientation)
    if (toOrientation == UIInterfaceOrientationLandscapeRight) toOrientation = UIInterfaceOrientationLandscapeLeft;
    else if (toOrientation == UIInterfaceOrientationLandscapeLeft) toOrientation = UIInterfaceOrientationLandscapeRight;

    UIInterfaceOrientation fromOrientation = [[UIApplication sharedApplication] statusBarOrientation];

    [self willRotateToInterfaceOrientation:toOrientation duration:0.0];
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self willAnimateRotationToInterfaceOrientation:toOrientation duration:[context transitionDuration]];
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self didRotateFromInterfaceOrientation:fromOrientation];
    }];

}


-(void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{

    NSLog(@"Transition");
}

您应该使用 orientations 列表添加 UISupportedInterfaceOrientation 键(您可以在目标的常规设置中使用 xCode UI 来实现)。 此外,如果您需要在不同的视图控制器上使用不同的方向,您可以重写方法

- (NSUInteger)supportedInterfaceOrientations

你应该阅读这个 adaptive interface,因为在 ios 8 中,方向改变意味着调整视图大小。

下面是如何在 iOS 8 上 Swift 中初始跟踪屏幕尺寸和方向变化的示例。

如果您刚刚开始,我会放弃特征集合委托方法。在 that 级别以编程方式处理约束和转换非常复杂且棘手。其中一些正在演变 and/or 没有很好的记录,并且在幕后发生约束转换很难弄清楚。你可能暂时不需要处理这样的事情。

如果您必须完全以编程方式调整约束,首先,通常在界面构建器中进行布局会更容易,使用约束,并将问题减少到对可能的约束数量进行微调代码,您可以在其中通过 IBOutlet 访问特定的约束,并修改它们的 .constant 属性(而不是 remove/replace 约束,这很难做到正确)。

如果您在 iOS 历史的这一点上刚刚起步,我认为 Swift 比 Objective C 更好(我已经做了很多Objective C 编程)Swift 是 Apple 的未来,它有一些不错的强大功能,同时解决了 Objective C 中的一些问题和挑战。它提供了许多便利。

    override func viewWillAppear(animated: Bool) {
        adjustViewLayout(UIScreen.mainScreen().bounds.size)
    }

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        adjustViewLayout(size)
    }

    func adjustViewLayout(size: CGSize) {
        var width = CGSize.width
        var height = CGSize.height

         /* this method will be called when the view is
            about to appear and whenever the orientation
            changes. Adjust what you need to here based
            on the incoming width and height

            you can infer whether the screen is in
            landscape or portrait by which dimension
            is larger.  Or use a switch statement
            to check for specific screen sizes
            and orientations.  Though it may
            be incomplete knowledge of layout or 
            a failure of design if you need to do that */

     }