2015-11-04 62 views
0

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

例如,我在我的收藏视图中显示肖像,我选择了一个图像(这是风景)。我检查它的宽度,并在我的viewPhoto的viewWillAppear中设置高度,以便将设备设置为横向。 “设备”旋转,以及。当我在人像后手动旋转它时,我希望它保持在横向,但不是。

这里是我的代码:

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") 

} 
+0

检查这一点,http://stackoverflow.com/questions/ 12640870/IOS -6-力设备取向 - 横向 – Vijay

回答

0

我修修补补这个解决方案,不正是我想要的,但它的工作:

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") 
     } 
    } 
}