使用按钮以编程方式更改方向-iOS

康斯坦丁诺斯(Konstantinos Natsios)

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

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

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

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

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

我没有运气尝试过以下代码。

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

当用户再次旋转手机时又回到纵向,有什么想法吗?

Hitesh surani

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

射影C

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

更新资料

迅捷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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用按钮以编程方式更改屏幕方向

iOS:使用IB创建按钮与以编程方式创建按钮

如何使用FlutterDriver以编程方式找到iOS后退按钮

以编程方式将方向从横向更改为纵向模式 xamarin IOS

iOS:以编程方式更改UIView的高度约束

以编程方式更改ImageView高度iOS

swift ios以编程方式设置PageViewController的过渡样式和方向

在iOS 8中以编程方式使按钮在视图使用约束中居中

iOS以编程方式使用GPUImageview截屏

使用“Assets.xcassets”中的图像文件以编程方式更改 iOS 应用程序图标

编程界面方向更改不适用于iOS

是否可以在iOS 7.1中以编程方式禁用“按钮形状”?

在iOS中以编程方式设置约束使按钮消失

iOS-如何以编程方式创建WKWebView后退按钮?

在iOs7中以编程方式设置按钮边框的颜色

以编程方式在swift ios中创建复选框按钮

以编程方式添加的按钮未在iOS中显示触摸感

使按钮以编程方式在iOS中播放声音

iOS-以编程方式更改约束关系

以编程方式更改iOS项目的Devices参数

iOS/Swift 4:从以编程方式创建的标签更改颜色

在iOS上以编程方式更改键盘类型无效

iOS-以编程方式更改CCFile Sprite图像

如何在iOS 11.4上以编程方式更改音量

iOS以编程方式更改字体大小不起作用

iOS:以编程方式更改从Interface Builder设置的约束

如何以编程方式更改iOS设备的音量?

iOS Swift以编程方式更改标签内的特定文本颜色

在iOS中以编程方式在应用中更改语言