How do I properly add UI elements into a view programmatically for Xamarin.iOS?

I am using Xamarin.iOS in Visual Studio 2013 to build a simple user interface programmatically in C#. I know it's much easier to use the designer view, but those aren't working for me right now and not an option.

So far, I managed to get the MPMoviePlayerController and the TextView showing, but the TextView won't scroll, even though I added requirements to do so. And the UILabel is no where to be seen. I thought adding each UI element as a subView of the main View, will show the elements in a stack order displaying the video at the top, label right under the video and textview with scroll. In a way, I want them to sort of stack each other layer by layer. What I am not doing right or am missing as I'm not too familiar with iOS?

using System;
using System.Drawing;

using Foundation;
using UIKit;
using MediaPlayer;

public partial class VideoViewController : UIViewController
{
    MPMoviePlayerController moviePlayer;
    UILabel label;
    UIScrollView scrollView;
    UITextView textView;
    public VideoViewController() : base("VideoViewController", null)
    {
    }

    public override void DidReceiveMemoryWarning()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning();

        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // TODO: Add landscape and portrait support with ScrollView text
        // Perform any additional setup after loading the view, typically from a nib.
        moviePlayer = new MPMoviePlayerController(NSUrl.FromString("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")); // Use FromString() to play video directly from web.
        moviePlayer.View.Frame = new CoreGraphics.CGRect(0, 20, this.View.Frame.Size.Width, 180); // size of the video frame
        moviePlayer.ScalingMode = MPMovieScalingMode.AspectFit; // show the video relative to the video size dimensions

        moviePlayer.PrepareToPlay();
        moviePlayer.Play();
        this.View.AddSubview(moviePlayer.View); // add the view after video starts playing to display it

        // UILabel
        label = new UILabel();
        label.Text = "Tutorial";
        label.Font.WithSize(36);
        this.View.AddSubview(label.ViewForBaselineLayout);

        // ScrollView
        scrollView = new UIScrollView();

        scrollView.ViewForBaselineLayout.Frame = new CoreGraphics.CGRect(0, 200, this.View.Frame.Size.Width,500);
        // UITextView
        textView = new UITextView();
        textView.Editable = false;
        textView.ScrollEnabled = true ;
        textView.ViewForBaselineLayout.Frame = new CoreGraphics.CGRect(0, 0, this.View.Frame.Size.Width, this.View.Frame.Size.Height);
        textView.Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. ";
        scrollView.ContentSize = scrollView.ViewForBaselineLayout.Frame.Size;

        scrollView.AddSubview(textView);
        this.View.AddSubview(scrollView);
    }
}

Best Answer

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Every view needs to have a size assigned to it. You should never do that in ViewDidLoad, though. I explain why here.

  • JaimePenzellnaJaimePenzellna USMember
    edited June 2015

    @adamkemp I read the article thoroughly and see what you mean now. It would be nice to show at least a visual example of how to put it all together with simple UI elements like UILabel, UITextView, and the like. The only progress I made was copying and pasting the above code into a ViewDidLayoutSubviews() to test and the scrollview worked, but the sizing of scroll was being cut off as I couldn't see what was further down. The UILabel still doesn't show and am trying to figure that out too.

    But thanks for the article, it helps a little! A visual coding example of the UI interaction with the view would be of great help!

  • A sample code in which a uiview is added to ViewController's view.

                    var sampleView = new UIView();
                    sampleView.Frame = new CoreGraphics.CGRect (100f,100f,200f,300f);
                    sampleView.BackgroundColor = UIColor.Blue;
                    this.View.AddSubview (sampleView);
    
  • AswathiPSAswathiPS USMember ✭✭

    @all any one please give me a sample application for UIView cross platforms implementation.

Sign In or Register to comment.