[Xamarin Blog] Using Custom UIViewControllers in Xamarin.Forms on iOS

MichaelBluesteinMichaelBluestein Michael BluesteinUSInsider, University, Developer Group Leader ✭✭✭
edited June 2014 in Xamarin.Forms

Just posted: Using Custom UIViewControllers in Xamarin.Forms on iOS on the Xamarin Blog.

Read the full article to learn how to work directly with native iOS UI code from Xamarin.Forms.

Use this thread to discuss or ask questions.

Posts

  • dannyg86dannyg86 Danny Glover IEMember

    A question unrelated to the topic, but related to the content of the blog post...

    Where do we get one of those Xamarin Monkey teddies? :)

  • rene_ruppertrene_ruppert René Ruppert USXamarin Team, University, XamUProfessors Xamurai

    @dannyg86 http://xamarin.com/sharp-shirt might be a good place to start looking!

  • dannyg86dannyg86 Danny Glover IEMember

    @rene_ruppert‌ Thank you!

    PS: Great work on Xamarin.Forms, I can't wait to sink my teeth into them.
    Cheers

  • RichardHopkinsRichardHopkins Richard Hopkins JPMember, University ✭✭✭

    Where do we get one of those Xamarin Monkey teddies? :)

    @dannyg86‌ You can win one in the Xamarin.Forms contest. Basically, you're almost guaranteed one because aside from the other prizes, all valid entries get a monkey. Hurry though - closing date for entries is Monday, June 16th.

    http://blog.xamarin.com/contest-build-your-first-xamarin.forms-app/

  • MichaelBluesteinMichaelBluestein Michael Bluestein USInsider, University, Developer Group Leader ✭✭✭

    A new version of Xamarin.Forms (version 1.1.0.6201) is now available in NuGet with some breaking changes to the renderer API. I updated the blog post and associated sample code to work with this latest version.

  • DerekWinnickiDerekWinnicki Derek Winnicki CAUniversity

    @MichaelBluestein‌ Nice post! I'm trying to take the post one step further and was wondering if you can answer my question. The question may seem unrelated to the post but I have modified UIViewControllers to be PageRenderers just like in the post so I'll ask here.

    I'm abstracting the iOS7 RESideMenu control into a Xamarin.Forms control. Everything is working except for one thing -- I need a reference to the SideMenuRenderer from my MenuRenderer. The Xamarin.Forms control is SideMenu and the class and renderer event are as follows:

    public class SideMenu : ContentPage
    {
        public Page MainPage { get; private set; }
        public Page MenuPage { get; private set; }
    
        public SideMenu (Page mainPage, Page menuPage)
        {
            MainPage = mainPage;
            MenuPage = menuPage;
        }
    }
    
    protected override void OnElementChanged (VisualElementChangedEventArgs e)
    {
        base.OnElementChanged (e);
        var sideMenu = e.NewElement as SideMenu;
        ContentViewController = sideMenu.MainPage.CreateViewController();
        MenuViewController = sideMenu.MenuPage.CreateViewController();
    }
    

    And the code from App.cs to use the control is:

        var menu = new Menu (menuPages);
        var sideMenu = new SideMenu (navigationPage, menu);
    

    First the SideMenuRenderer is called and secondly the MenuRenderer. The original iOS implementation used a UIViewController extension method which iterated through ParentViewController's until it found the SideMenuViewController. The problem is the ParentViewController is null for MenuRenderer in this extension method.

    I even explicitly called AddChildViewController(MenuViewController) in the SideMenuRenderer so I know that the parent is set on MenuViewController, but it seems that MenuRenderer is a separate instance from the ViewController created by sideMenu.MenuPage.CreateViewController() since the Parent is always null. Any idea how to implement this correctly?

  • CraigDunnCraigDunn Craig Dunn USForum Administrator, Xamarin Team Xamurai

    Any chance you can just use the Xamarin.Forms MessagingCenter to communicate between the two views?

  • DerekWinnickiDerekWinnicki Derek Winnicki CAUniversity

    @CraigDunn That's not a bad idea. I'll try that out and get back to you. Thanks.

  • DerekWinnickiDerekWinnicki Derek Winnicki CAUniversity

    The MessagingCenter is awesome and took very little code to hook it up! Thanks!

    In my SideMenuRender OnElementChanged:

        MessagingCenter.Subscribe<MenuRenderer.MenuSource, UIViewController> (this, "MenuItemSelected", (sender, vc) => {
        if (ContentViewController != vc)
            SetContentViewController (vc, true);
        HideMenuViewController ();
        });
    

    And my MenuRenderer:

    private void DisplayViewController(short index)
    {
        MessagingCenter.Send<MenuSource, UIViewController> (this, "MenuItemSelected", controllers[index]);
    }
    
  • KrishnaPatelKrishnaPatel Krishna Patel INMember

    What would be the best approach for implementing image cropping for Android with Xamarin.Forms?

  • DerekWinnickiDerekWinnicki Derek Winnicki CAUniversity

    Can I use the PageRenderer approach with UICollectionViewController?

  • CraigDunnCraigDunn Craig Dunn USForum Administrator, Xamarin Team Xamurai

    @DerekWinnicki‌ check elsewhere on the forums as I think someone has already tried that. Also search Bugzilla, as I believe they hit a wall trying to get the touch events working in the current version of Xamarin.Forms.

  • DerekWinnickiDerekWinnicki Derek Winnicki CAUniversity

    @CraigDunn Thanks. I found the thread you're referring too. I had the same issue as them because initially I tried rendering the UICollectionView with ViewRenderer<TView, TNativeView>. The control displayed fine but wouldn't respond to ItemSelected events. I thought I was taking the wrong approach by rendering the UICollectionView and not the UICollectionViewController. Now I'm not quite sure which is correct. I'll keep working on it.

  • MichaelBluesteinMichaelBluestein Michael Bluestein USInsider, University, Developer Group Leader ✭✭✭
  • DerekWinnickiDerekWinnicki Derek Winnicki CAUniversity

    Okay I guess I was taking the right approach then, thanks.

  • BrettAllredBrettAllred Brett Allred USMember

    Thank you for the post. I have things working per your instruction but I have run into an issue. I am implementing the Xamarin Salesforce SDK and in their SDK they have a UIViewController for the login screen. From the SalesforceClient you get a reference to the LoginInterface UIViewController then use the PresentViewController Method to show it.

    var loginController = Client.GetLoginInterface () as UIViewController;
    PresentViewController (new UINavigationController(loginController), true, null);

    After authentication there is a call back function where inside you can dismiss the presented view controller.

    DismissViewController (true, () => {
                    // omitted
                });

    I have the code working if I use a Regular UIViewController but I am trying to get the concept working with Xamarin.Forms and the page renderer.

    I am assuming the PresentViewController doesn't work in a PageRenderer.
    Is there a way to Pop a UiViewController on to the NavigationPageStack so it gets presented?

    Any other ideas on how I can get this working ?

  • AbujaDeveloperAbujaDeveloper Abuja Developer USMember

    How do you create a PageRenderer for a UniversalViewController?

  • BruceHamilton.6835BruceHamilton.6835 Bruce Hamilton USUniversity

    I tried this approach but I'm still trying to find out how you can have iOS create and add a new View to a ViewController, in response to button click event in Xamarin forms. Essentially, I just want to create a Xamarin Forms application with a "Draw Box" button (in the MainPage.cs file of the PCL) and then have iOS and Android respond to it by their using native graphic APIs to draw a simple box.

    I'm doing iOS first, and I'm working with the iOS ViewControllers and drawing graphics okay but coordinating them with the shared code is where I'm having trouble. I also tried a dependency injection and that doesn't work.

    Suggestions? Thx!

  • JoseManuelOjedaJoseManuelOjeda Jose Manuel Ojeda SVMember
    edited April 2015

    hey @BrettAllred I'm facing the same problem trying to use xamarin forms and the https://components.xamarin.com/view/xamarin.auth ,I don't know how to navigate back to a xamarin form page after I logged in

    any suggestion @MichaelBluestein @CraigDunn

  • BrettAllredBrettAllred Brett Allred USMember

    @JoseManuelOjeda - It has been a while since I was working on this but I went back to my app and found I had used

    PresentViewController (App.GetMainPage ().CreateViewController (), true, null);
    

    GetMainPage just returns a ContentPage

    Hope that helps.

  • JoseManuelOjedaJoseManuelOjeda Jose Manuel Ojeda SVMember

    thanks @BrettAllred I'll give it a try and let you know the results

  • FujikatomaFujikatoma Lars Hallek USMember, University
    edited September 2015

  • AdrianKnightAdrianKnight Adrian Knight USMember ✭✭✭✭

    @MichaelBluestein Can you post a version of the source code where cropper view derives from UIViewController instead of PageRenderer? This is because I have a custom UIViewController and I'd like to know how it could be used for Xamarin Forms.

  • CharanPottabathiniCharanPottabathini Charan Pottabathini INMember

    We are trying to implement this and ran into few issues.

    We need image should be fit in the screen but given example make hidden if image is overflow. We achieved that using image height and width to fit in the screen but ran into another issue.

    When we make size fit in the screen after crop giving another portion of the picture not the actual where we put crop. We understood that because of we make fit image into the screen.

    Can anyone give solution for this.

  • JuanJativaJuanJativa Juan Jativa ECMember

    @CharanPottabathini said:
    We are trying to implement this and ran into few issues.

    We need image should be fit in the screen but given example make hidden if image is overflow. We achieved that using image height and width to fit in the screen but ran into another issue.

    When we make size fit in the screen after crop giving another portion of the picture not the actual where we put crop. We understood that because of we make fit image into the screen.

    Can anyone give solution for this.

    I'm having the same issue. Did you find something?

Sign In or Register to comment.