我正在尝试在我的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 :(得分: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")
}
}
}