LoginSignup
10
8

More than 5 years have passed since last update.

Xamarin.iOSでのストーリーボードを使わない開発

Last updated at Posted at 2017-11-16

 Visual Studio で Xamarin.iOS のプロジェクトを作成すると、ストーリーボードなどという忌まわしいものがデフォルトで作成されてしまい非常に厳しい気持ちになります。この記事では、それらを削除して、コードでUIを構成していくためのステップを説明します。3分ほど作業をすれば邪魔なものが消えてなくなってくれます。

1. Main.storyboard を削除

2. ViewController.designer.cs を削除

3. ViewController の余計なコードを削除する

ViewController が partial class である必要がなくなったので、partial キーワードを消します。合わせて IntPtr を引数にとるコンストラクタも不要になりました。

using System;

using UIKit;

namespace Pawotter.iOS
{
-   public partial class ViewController : UIViewController
+   public class ViewController : UIViewController
    {
-       protected ViewController(IntPtr handle) : base(handle)
-       {
-           // Note: this .ctor should not contain any initialization logic.
-       }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }
}

4. Info.plist の Main Interface を削除

5. AppDelegate に ViewController を表示するコードを追加

using Foundation;
using UIKit;

namespace Pawotter.iOS
{
    [Register("AppDelegate")]
    public class AppDelegate : UIApplicationDelegate
    {
        public override UIWindow Window { get; set; }

        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
+           Window = new UIWindow(UIScreen.MainScreen.Bounds);
+           var vc = new UIViewController();
+           vc.View.BackgroundColor = UIColor.Orange;
+           Window.RootViewController = vc;
+           Window.MakeKeyAndVisible();
            return true;
        }
    }
}

6. 動くサンプル

GitHub で公開しているので適当にどうぞ: https://github.com/pawotter/Xamarin.iOS.NonStoryboard

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8