2015-11-04 61 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") 
     } 
    } 
}