简体   繁体   English

iOS >>更改设备方向后更换笔尖

[英]iOS >> Replacing Nib Upon Device Orientation Change

In a certain scenario, I wish to replace the presented Nib for a View Controller upon Device Orientation Change. 在某些情况下,我希望在设备方向发生变化时替换为视图控制器提供的Nib。

Something like this: 像这样:

在此处输入图片说明在此处输入图片说明

I wish for both of the Nib files to be owned by the same View Controller, so they react to the same IBActions, IBOutlets, etc... 我希望两个Nib文件都由同一View Controller拥有,以便它们对相同的IBAction,IBOutlets等做出反应。

Also, Auto Layout will be a bit complicated for this specific scenario (the images I've used here are just to demonstrate the issue - the actual implementation is more complicated...). 另外,对于这种特定情况,自动版式会有些复杂(我在这里使用的图像只是为了演示这个问题-实际的实现更加复杂...)。

Apple example for this scenario is problematic as it uses a Segue for this action. 苹果公司在这种情况下的示例是有问题的,因为它使用Segue来执行此操作。 If, for example, the VC is part of a Navigation Controller stack, it will make it very problematic to react to the Back button click - instead of just Popping the VC I will have to Pop 2 VCs (I think): 例如,如果VC是导航控制器堆栈的一部分,那么对“后退”按钮单击做出反应将非常成问题-而不是仅弹出VC,我将不得不弹出2个VC(我认为):

在此处输入图片说明

Intuitively, I'm thinking about replacing the Nib file (and not performing a Segue). 凭直觉,我正在考虑替换Nib文件(而不执行Segue)。

Is it a valid action? 这是有效的措施吗?

If so, how should I perform it in code? 如果是这样,我应该如何在代码中执行它?

Also, can it be achieved in Storyboard, or do I need to use a .xib file? 另外,可以在Storyboard中实现此功能,还是需要使用.xib文件?

Autolayout si perfect for this scenario, the "only" problem is that you can't do in Storyboard Editor, but in code. Autolayout非常适合这种情况,“唯一”的问题是您不能在Storyboard Editor中执行,而只能在代码中执行。 I really suggest you a book there is an equal problem Erica Sadun Autolayout demistified. 我真的建议您一本书,Erica Sadun Autolayout有同样的问题。
The approach is to have different sets of constraints, one for landscape and one portrait, removing and installing them alternatively, but maybe the best would be just change their priorities. 方法是有不同的约束集,一个约束用于风景和一幅肖像,或者交替删除和安装它们,但是最好的办法就是更改它们的优先级。
Changing the xib seems an easier solution, but in my opionio is a less maintinable options. 更改xi​​b似乎是一个更简单的解决方案,但是在我看来,这种解决方法不太容易维护。
You should: 你应该:

  • create 2 xibs: one for portrait an one for landscape 创建2个xib:一个用于肖像,一个用于风景
  • on the view controller's xib, create a container view, with autoresizing masks or constraint that will stretch it according to main view changes. 在视图控制器的xib上,创建一个容器视图,该容器视图具有自动调整大小的遮罩或约束,可以根据主视图的变化对其进行拉伸。
  • in the view controller method - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duratio n swap the 2 view. 在视图控制器方法中- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duratio n交换两个视图。 since this is wrapped in an animation block, you can just lower alpha on one a turn on on the other and remove 由于这是包裹在动画块中的,因此您可以只降低一个开关的alpha值,然后移除

Take a look a here 看看这里

You can do something like this : 您可以执行以下操作:

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(devOrientation) && !self.viewIsLandscape ) {
        [self presentModalViewController:self.landscapeVC animated:YES];
        self.viewIsLandscape = YES;
    } else if (UIDeviceOrientationIsPortrait(devOrientation) &&
               self.viewIsLandscape)
    {
        [self dismissModalViewControllerAnimated:YES];
        self.viewIsLandscape = NO;
    }
}

Ref : http://www.edumobile.org/iphone/iphone-programming-tutorials/orientation-changes-for-iphone/ 参考: http : //www.edumobile.org/iphone/iphone-programming-tutorials/orientation-changes-for-iphone/

Hope this will help you... 希望这个能对您有所帮助...

You can create two separate UIViews inside single XIB instead of two different xibs. 您可以在单个XIB内创建两个单独的UIView,而不是两个不同的Xib。 And you can change the view based on change of orientation. 您可以根据方向的变化来更改视图。

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

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