gpt4 book ai didi

ios - 强制 ipad 方向为一个 View

转载 作者:行者123 更新时间:2023-11-30 13:56:52 26 4
gpt4 key购买 nike

我试图在我的 iPad 应用程序中强制确定一个 View 的方向,但我无法保持它的持久性。

例如,我在收藏 View 中处于纵向,我选择一个图像(横向)。我在 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")

}

最佳答案

我修改了这个解决方案,不完全是我想要的,但它完成了工作:

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

关于ios - 强制 ipad 方向为一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com