0

I am making one custom keyboard in which when orientation of mobile is changed then in landscape i have to hide one button please suggest how can i do this i am using below code to do this task please help me. in landscape also the button is visible i want to hide button on landscape and visible in portrait mode

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
     }
}
aff
  • 162
  • 2
  • 6
  • 17
  • You wont get the code here. Please state what your problem is. Don't ask how to code it. – qwerty_so Jan 07 '15 at 10:17
  • my problem is orientation is change but in landscape mode also the button is not hidden –  Jan 07 '15 at 10:25

1 Answers1

1

In your viewDidLoad put

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

Then add this method

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

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

}

Note:

UIDevice.currentDevice().orientation might not give you the correct orientation. You can use the status bar orientation instead UIApplication.sharedApplication().statusBarOrientation

From the apple docs:

UIDevice.currentDevice().orientation

The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application’s user interface. See “UIDeviceOrientation” for descriptions of the possible values.

Y2theZ
  • 10,162
  • 38
  • 131
  • 200
  • did you try `UIApplication.sharedApplication().statusBarOrientation` instead of `UIDevice.currentDevice().orientation` ? – Y2theZ Jan 07 '15 at 10:36
  • No i have not try can you please let me know how to implement this –  Jan 07 '15 at 10:41
  • The same code Above but replace `UIDevice.currentDevice().orientation` with `UIApplication.sharedApplication().statusBarOrientation` – Y2theZ Jan 07 '15 at 10:46
  • hi it show error if(UIDeviceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation)) –  Jan 07 '15 at 10:50
  • try `UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOri‌​entation)` – Y2theZ Jan 07 '15 at 10:59
  • UIApplication does not have a member name 'statusBarOrientation' –  Jan 07 '15 at 11:11