2015-11-04 2 views
0

Я пытаюсь заставить ориентацию для одного вида в своем приложении ipad, но я не могу сохранить его постоянным.Принудительная ориентация ipad для одного вида

Например, я нахожусь в портрете в своем представлении коллекции, я выбираю изображение (которое является ландшафтом). Я проверяю его ширину и высоту в viewWillAppear моего 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") 

} 
+0

Проверить это, http://stackoverflow.com/questions/ 12640870/КСН-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") 
     } 
    } 
} 
Смежные вопросы