0

我正在制作一个自定义键盘,当移动设备的方向发生变化时,我必须在横向中隐藏一个按钮,请建议我该怎么做我正在使用下面的代码来完成这项任务,请帮助我。在横向中该按钮也是可见的 我想在横向上隐藏按钮并在纵向模式下可见

override func updateViewConstraints() {
    super.updateViewConstraints()

   //  Add custom view sizing constraints here

    var currentDevice: UIDevice = UIDevice.currentDevice()
    var orientation: UIDeviceOrientation = currentDevice.orientation

    if orientation.isLandscape {

     button.hidden = true
     }

    if orientation.isPortrait {
        button.hidden = false
     }
}
4

1 回答 1

1

在你的viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged", name: UIDeviceOrientationDidChangeNotification, object: nil)

然后添加这个方法

func orientationChanged()
{
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {            
        button.hidden = true
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        button.hidden = false
    }

}

笔记:

UIDevice.currentDevice().orientation可能不会给你正确的方向。您可以改用状态栏方向UIApplication.sharedApplication().statusBarOrientation

来自苹果文档:

UIDevice.currentDevice().orientation

该属性的值是一个常数,指示设备的当前方向。此值表示设备的物理方向,可能与应用程序用户界面的当前方向不同。有关可能值的描述,请参阅“UIDeviceOrientation”。

于 2015-01-07T10:29:40.980 回答