Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Detecting the device orientation


In this recipe, we will learn how to make an app that is aware of changes in the device orientation.

Getting ready

Create a new Single View Application in Xamarin Studio and name it DeviceOrientationApp.

How to do it...

Perform the following steps:

  1. Add a label to the controller.

  2. In the DeviceOrientationAppViewController class, override the ViewWillAppear method and implement it with the following code:

    private NSObject orientationObserver;
    public override void ViewWillAppear (bool animated)
    {
      base.ViewWillAppear (animated);
      UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
      this.orientationObserver = UIDevice.Notifications.ObserveOrientationDidChange((s, e) => {
        this.lblOrientation.Text = UIDevice.CurrentDevice.Orientation.ToString();
      });
    }
  3. Override the ViewWillDisappear method by using the following code:

    public override void ViewWillDisappear (bool animated)
    {
      base.ViewWillDisappear (animated);
      NSNotificationCenter.DefaultCenter...