【问题标题】:Changing RootView controller Xamarin IOS app更改 RootView 控制器 Xamarin IOS 应用程序
【发布时间】:2020-06-25 00:11:48
【问题描述】:

我的 Xamarin IOS 项目使用 Storyboard。它是一个 tabbarcontroller 应用程序,我想修改我的根 UITabBarController 中的选项卡数。

如果从 Storyboard 中创建 tabbarcontroller,则无法添加或删除选项卡。我想用不是从情节提要创建的根视图控制器替换根视图控制器。我仍然喜欢我的其他一些课程的故事板。

在 Mac 上使用 Visual Studio 创建空 Xamarin 项目或从 Xamarin 项目中删除情节提要的说明不起作用。

我认为新的 SceneDelegate 移除了在 AppDelegate 中设置根视图控制器的功能。

谢谢, 格里

【问题讨论】:

    标签: xamarin.ios storyboard uitabbarcontroller


    【解决方案1】:

    其实你不需要删除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");
    

    【讨论】:

    • 谢谢。那行得通。我知道 AppDelegate 修改,我知道是 SceneDelegate 搞砸了,但我找不到让它工作的咒语。现在代码显然是多余的 - 但可以正常工作。
    • 现在我注意到在 SceneDelegate 中更改 rootViewController 后,我的应用程序禁用了文本输入。我错过了什么?谢谢。
    • 您可以创建一个包含更多详细信息的新主题,以便人们可以更好地帮助您。
    • 你可以分享你的样品,这样我就可以在我身边测试它
    猜你喜欢
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多