Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当设备方向更改时更改ViewController?_Ios_Swift_Viewcontroller - Fatal编程技术网

Ios 当设备方向更改时更改ViewController?

Ios 当设备方向更改时更改ViewController?,ios,swift,viewcontroller,Ios,Swift,Viewcontroller,我想知道当方向改变时如何切换VCs。在同一个故事板中,我有两个不同的场景:肖像VC和景观VC。我们的目标是让基本上相同的事情发生在风景中,但使用不同的UIImage。有人需要帮忙吗 逻辑应该是这样的: if orientation is landscape { use landscapeVC } else { Use portraitVC } 好的,我不太确定你到底想实现什么,但这里有两种可能的解决方案: 1.在旋转时更改UIImageView图像 您可以添加观察者,以便在设

我想知道当方向改变时如何切换VCs。在同一个故事板中,我有两个不同的场景:肖像VC和景观VC。我们的目标是让基本上相同的事情发生在风景中,但使用不同的UIImage。有人需要帮忙吗

逻辑应该是这样的:

if orientation is landscape {
    use landscapeVC 
} else {
    Use portraitVC
}

好的,我不太确定你到底想实现什么,但这里有两种可能的解决方案:

1.在旋转时更改UIImageView图像 您可以添加观察者,以便在设备更改方向时收到通知

将此添加到viewDidLoad():

方向更改时将调用以下函数:

func didRotate(notification: NSNotification) {
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
        // Change UIImageView image here
    }

    if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation) {
        // Change UIImageView image here
    }
}
您可以使用此代码更改纵向或横向的图像。哦不要忘记删除视图中的观察者WillEnglishe():

2.在旋转时更改布局和UI 这有点棘手,但一点也不难。您需要使用故事板大小类

签出上面图像的中下部。看看它在哪里写着“wCompact hRegular”,这意味着你为所有iPhone设计的肖像。对于景观中的iPhone,请使用wAny hCompact。查看如何禁用上图中的某些约束?这是因为它们不适用于特定大小的类

通过使用大小类,您不仅可以为纵向和横向定制UI,还可以为更具体的设备(iPhone和iPad)定制UI。关于这个话题,你可以多读一点


我希望这有帮助!:)

只有说明问题的post代码@意思很重要,整个代码就是问题所在。我真的不知道该修复什么,包括每一个图像初始化、潦草的空行和注释掉的代码?@如果我对整个程序有问题,这很重要,是的,我确实认为整个代码都需要发布。我知道你是从哪里来的,但是为了让我的大脑能够理解这里教给我的东西,我想让响应者看到我正在处理的一切。哇。使用大小类是完美的解决方案。我花了好几个小时试图找出代码来精确地实现类的大小。非常感谢您的解决方案。
func didRotate(notification: NSNotification) {
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
        // Change UIImageView image here
    }

    if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation) {
        // Change UIImageView image here
    }
}
NSNotificationCenter.defaultCenter().removeObserver(self)