Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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 强制一个视图的ipad方向_Ios_Swift_Ipad - Fatal编程技术网

Ios 强制一个视图的ipad方向

Ios 强制一个视图的ipad方向,ios,swift,ipad,Ios,Swift,Ipad,我试图在我的ipad应用程序中强制一个视图的方向,但我无法保持它的持久性 例如,我在我的收藏视图中的肖像中,我选择了一幅风景画。我在viewPhoto的视图中检查它的宽度和高度,以便将设备设置为横向。这个装置可以旋转。当我将它手动旋转到纵向后,我希望它保持在横向,但它没有 这是我的密码: override func viewWillAppear(animated: Bool) { // Remove hairline on toolbar mytoolBar.clipsToBou

我试图在我的ipad应用程序中强制一个视图的方向,但我无法保持它的持久性

例如,我在我的收藏视图中的肖像中,我选择了一幅风景画。我在viewPhoto的视图中检查它的宽度和高度,以便将设备设置为横向。这个装置可以旋转。当我将它手动旋转到纵向后,我希望它保持在横向,但它没有

这是我的密码:

override func viewWillAppear(animated: Bool) {
    // Remove hairline on toolbar
    mytoolBar.clipsToBounds = true

    // Load image from svg
    let img = getImageFromBase64(arrayBase64Images[index])
    imgView.image = img

    var value: Int
    if ( img.size.width > img.size.height ) { // turn device to landscape
        value = UIInterfaceOrientation.LandscapeRight.rawValue
    }
    else { // turn device to portrait
        value = UIInterfaceOrientation.Portrait.rawValue
    }
    UIDevice.currentDevice().setValue(value, forKey: "orientation")

}

我修补了这个解决方案,这并不完全是我想要的,但它确实起到了作用:

override func viewWillAppear(animated: Bool) {
    // Remove hairline on toolbar
    mytoolBar.clipsToBounds = true

    // Load image from svg
    let img = getImageFromBase64(arrayBase64Images[index])
    imgView.image = img

    var value: Int

    if ( img.size.width > img.size.height ) { // turn device to landscape
        if( !UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) )
        {
            value = UIInterfaceOrientation.LandscapeRight.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }
        landscape = true
    }
    else { // turn device to portrait
        if( !UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation) )
        {
            value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }
        landscape = false
    }

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)

}

func rotated()
{
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {
        if ( !landscape ) {
            let value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        if ( landscape ) {
            let value = UIInterfaceOrientation.LandscapeRight.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }
    }
}
看看这个,