简体   繁体   English

显示视图时的方向处理

[英]Handle orientation when showing a view

I have a few changes that get made to a UIView when the orientation changes. 当方向更改时,我对UIView进行了一些更改。 This works fine. 这很好。 The problem arises when adding the view after the phones orientation is already switched. 在切换电话方向后添加视图时会出现问题。 This causes none of the rotate methods to be called and therefor does not give me the opportunity to make changes. 这导致没有任何旋转方法被调用,因此没有给我机会进行更改。

What would be the correct way of handling this, probably in ViewDidLoad? 可能在ViewDidLoad中处理此问题的正确方法是什么? Would I be able to detect the current orientation at that point? 我能在那个时候检测到当前方向吗?

Bare in mind that its a few minor changes that I would need to make, so I dont want to load a different nib or anything like that 切记,我需要进行一些小的更改,所以我不想加载其他笔尖或类似的东西

Thanks you very much :) 非常感谢你 :)

EDIT* just to clear things up: As I mentioned, the view is not even instantiated yet when the device orientation changes. EDIT *只是为了清除问题:正如我提到的,当设备方向改变时,该视图甚至都没有实例化。 The orientation changes to landscape -> the user clicks a button that shows another view -> this new view gets created and shown, but its default positioning is for portrait orientation -> when the view is shown, the elements I rearrange in the willAnimateRotationToInterfaceOrientation method are in the wrong position. 方向变为横向->用户单击显示另一个视图的按钮->创建并显示此新视图,但其默认位置为纵向->显示该视图时,我将在willAnimateRotationToInterfaceOrientation方法中重新排列元素位置错误。

Typically I put the animations that occur when the user rotates the device (manipulating the frames of views mostly) in the willAnimateToInterfaceOrientation method. 通常,我将在用户旋转设备(主要操作视图的帧)时出现的动画放在willAnimateToInterfaceOrientation方法中。 In it's skeleton form it would look like this: 它的骨架形式如下所示:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    //NSLog(@"willAnimateRotationToInterfaceOrientation: %d", toInterfaceOrientation);

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        // portrait
    }
    else
    {
        // landscape
    }
}

EDIT: In situations where I need to remember the device rotation for future use, I set up an ivar in my view controller class called currentOrientation (type int), and then do this: 编辑:在需要记住设备旋转以备将来使用的情况下,我在名为currentOrientation(类型为int)的视图控制器类中设置了一个ivar,然后执行以下操作:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    //NSLog(@"shouldAutorotateToInterfaceOrientation: %d", toInterfaceOrientation);

    if (toInterfaceOrientation == UIDeviceOrientationPortrait || toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown ||
        toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
    {
        currentOrientation = toInterfaceOrientation;
    }

    return YES;
}

Then while running the methods in the view controller, I know which orientation the device is in. 然后,在视图控制器中运行方法时,我知道设备所处的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM