其实你不需要删除Storyboard。如果要在 AppDelegate 中设置RootViewController,请查看以下代码。
在 Appdelegate 中
以下代码将在 iOS 13.0 之前运行
public bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
this.Window = new UIWindow(UIScreen.MainScreen.Bounds);
var MainViewController = new MyViewController();
this.Window.RootViewController = MainViewController;
this.Window.MakeKeyAndVisible();
return true;
}
而在iOS 13.0之后,我们应该调用SceneDelegate中类似的代码,所以在SceneDelegate中同时添加如下代码。
public void WillConnect (UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
this.Window = new UIWindow(new UIWindowScene(session,connectionOptions));
var MainViewController = new MyViewController();
this.Window.RootViewController = MainViewController;
this.Window.MakeKeyAndVisible();
// This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).
}
另外,如果您想在运行时更改 RootViewController(例如单击按钮时)。
我们使用动画来使过程流畅
var MainController = new UITabBarController();
CATransition transition = CATransition.CreateAnimation();
transition.Duration = 0.3;
transition.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseOut);
UIApplication.SharedApplication.KeyWindow.RootViewController = MainController;
UIApplication.SharedApplication.KeyWindow.Layer.AddAnimation(transition, "Animation");