使用按钮以编程方式更改方向-iOS [英] Change orientation programmatically with button - iOS

查看:89
本文介绍了使用按钮以编程方式更改方向-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器,我希望有以下经验. 在视图控制器内部,我有一个按钮,该按钮强制将方向旋转到正确的横向.

I have a view controller and I would like to have the following experience. Inside the view controller i got a button which force rotate the orientation to landscape right.

在部署信息-设备方向"中,我启用了横向右"和纵向".

In Deployment Info - Device Orientation i have enabled "Landscape Right" and "Portrait".

我想提到的是,我在设备中启用了纵向方向锁定",这就是为什么我希望按钮使用以下代码以编程方式旋转方向的原因.

I want to mention that in my device i have enabled the "Portrait Orientation Lock" that's why i want a button to rotate the orientation programmatically with the following code.

let rotateButton: UIButton = {
    let btn = UIButton(type: .system)
    btn.setTitle("Rotate", for: .normal)
    btn.setTitleColor(.red, for: .normal)
    btn.addTarget(self, action: #selector(rotateTapped), for: .touchUpInside)
    return btn
}()

@objc func rotateTapped() {
    let value = UIInterfaceOrientation.landscapeRight.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

因此上面的代码正常工作,并且屏幕正在旋转. 虽然我想在用户旋转回纵向时将屏幕再次旋转至纵向,而无需按下任何按钮. 启用纵向锁定"后,屏幕不会旋转回纵向.

So the code above works properly and the screen is rotating. Although i want when the user rotates back to Portrait, the screen to rotate to portrait again without any button pressed. When the "Portrait Orientation Locked" is on, the screen is not rotating back to portrait.

我尝试了以下代码,但是没有运气.

I've tried the following codes with no luck.

1)

    NotificationCenter.default.addObserver(self, selector: #selector(rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)


@objc func rotated() {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape") //when the user taps the button this is being printed.
    } else {
        print("Portrait") //when the user rotates back to portrait this is NOT being printed.
    }
}

和2)

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation == .landscapeRight {
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }
}

关于用户再次旋转手机会变回肖像的任何想法?

Any idea of what can it be to change back to portrait when user rotates the phone again?

推荐答案

我没有快速代码.下面是objective c代码,它为我工作.您可以根据需要将其转换为快捷方式.

I don't have a swift code. Below are the objective c code, It worked for me. You can convert it into swift as per your requirement.

Ojective C

UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];    
[UIViewController attemptRotationToDeviceOrientation];

更新

Swift 4.0

var value : Int = UIInterfaceOrientation.landscapeRight.rawValue
if UIApplication.shared.statusBarOrientation == .landscapeLeft || UIApplication.shared.statusBarOrientation == .landscapeRight{
   value = UIInterfaceOrientation.portrait.rawValue
}

UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

以上代码已由我测试过,并且工作正常.如果上面的代码对您不起作用,请在一段时间后使用performSelector执行它.

Above code already tested by me and it is working fine. Incase above code is not worked for you, Then execute it after some delay using performSelector.

这篇关于使用按钮以编程方式更改方向-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆