Issue
In my solution, I have one iOS project that supports both iPads and iPhones. I would like to split it to have
- iOS shared project (shared views and services for both iPads and iPhones)
- iPad project with reference to the shared one
- iPhone project with reference to the shared one
I had troubles doing that, so
- I created an empty Xamarin.iOS (for iPhone/iPad) project (with Visual Studio template) and added Class Library project (for shared logic)
- I've added a needed reference.
- Created
TestViewController
inLibrary
project (simple one with a label only). No storyboard behind it. Just
public class TestViewController : UIViewController
{
public TestViewController()
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.View.BackgroundColor = UIColor.Cyan;
var label = new UILabel
{
TranslatesAutoresizingMaskIntoConstraints = false,
Text = "Love Santa Claus"
};
View.Add(label);
label.CenterXAnchor.ConstraintEqualTo(View.CenterXAnchor).Active = true;
label.CenterYAnchor.ConstraintEqualTo(View.CenterYAnchor).Active = true;
}
}
- Navigate to that ViewController in
AppDelegate
the problem is that the app gets stuck on the LaunchScreen
. I put break point in line
mainNavController.PushViewController(new TestViewController(), true);
. Then TestViewController constructor is called but ViewDidLoad
is not getting called in TestViewController
.
I'm looking for ideas about what did I miss.
Solution
If you want to set TestViewController
as the first view controller, you could set as follows:
...
UINavigationController navigationController = new UINavigationController(new TestViewController());
Window.RootViewController = navigationController;
...
in Appdelegate
the following code will work before 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);
UINavigationController navigationController = new UINavigationController(new TestViewController());
this.Window.RootViewController = navigationController ;
this.Window.MakeKeyAndVisible();
return true;
}
And after iOS 13.0 , we should call the similar code in SceneDelegate
, so add the following code to SceneDelegate at same time .
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));
UINavigationController navigationController = new UINavigationController(new TestViewController());
this.Window.RootViewController = navigationController;
this.Window.MakeKeyAndVisible();
// This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).
}
Answered By - Junior Jiang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.