Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

AutoLayout implementation in C# Visual Studio 2013 without using Interface Builder or .nib

$
0
0

I started using Xamarin because I wanted to stay in the Visual Studio 2013 environment and not have to learn a new environment or language. I just discovered AutoLayout. It seems to me that AutoLayout understanding is critical to speeding up development. However, I'm not finding a lot of information for using AutoLayout with pure C# in Visual Studio 2013. Perhaps I'm just not looking in the right places.

Anyway, Let's start this new discussion with a simple controller that uses AutoLayout TOTALLY in C# without using any .nib or Interface Builder stuff at all. And without using Xamarin Studio. Just ALL done in Visual Studio 2013.

Here are the requirements for our first attempt:

  1. Make a UIViewController that will facilitate implementation of apple's iAD.
  2. Place an iAD banner at the bottom of the screen taking up the full width.
  3. Put the main app view above the iAD banner and have it fill the rest of the screen.

Thinks to consider:

  1. The banner view may disappear from time to time if no iADs are present, so we need to fill that space when there are no iADs.
  2. We need to handle when the device rotates to accommodate new orientation.
  3. We need to handle different screen sizes for different devices. iPad, iPod, iPhone, 4th and 5th generation.

The above implementation should be trivial, but I've been banging my head on the keyboard for 2 days trying to get this to work. Any recommendations, examples, or ideas would be GREATLY HELPFUL. My implementation below results in my app view taking up the left half of the screen, and the iAD not appearing at all because it is below the main app view (vertically) and outside of the lower screen bounds. What's going on here? Do I need to turn on the AutoLayout Feature somewhere? Can I do it in C# code or is it hiding somewhere in Project Settings?

Here is my non working attempt:

using System; 
using MonoTouch.iAd; 
using MonoTouch.UIKit;

namespace ADControllerHelperiOS
{
  public class ADViewController : UIViewController
  {
    private UIView InternalView { get; set; } private ADBannerView AdView { get; set; }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        InternalView = new UIView{BackgroundColor=UIColor.Blue}; 

        //This is apple's standard ADBannerView
        AdView = new ADBannerView(ADAdType.Banner) {Hidden = true};
        AdView.FailedToReceiveAd += HandleFailedToReceiveAd;
        AdView.AdLoaded += HandleAdLoaded;

        View.BackgroundColor = UIColor.Clear;

        //I'm pretty sure that we need these three lines
        View.TranslatesAutoresizingMaskIntoConstraints = false;
        InternalView.TranslatesAutoresizingMaskIntoConstraints = false;
        AdView.TranslatesAutoresizingMaskIntoConstraints = false;

        View.AddSubview(InternalView);
        View.AddSubview(AdView);

        Resize();
    }

    public override void DidRotate(UIInterfaceOrientation fromInterfaceOrientation)
    {
        base.DidRotate(fromInterfaceOrientation);
        Resize();
    }

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        Resize();
    }

    private void Resize()
    {
        //Remove all constraints, and reset them...
        View.RemoveConstraints(View.Constraints);

        if (AdView == null || AdView.Hidden)
        {//Fill up the entire screen with our InternalView
            View.AddConstraint(NSLayoutConstraint.Create(InternalView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1, 0));
            View.AddConstraint(NSLayoutConstraint.Create(InternalView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, 0));
            View.AddConstraint(NSLayoutConstraint.Create(InternalView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, 0));
        }
        else
        {//Put banner ad at the bottom of the screen and fill the rest of the screen with our InternalView
            View.AddConstraint(NSLayoutConstraint.Create(AdView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1, 0));
            View.AddConstraint(NSLayoutConstraint.Create(InternalView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1, 0));

            View.AddConstraint(NSLayoutConstraint.Create(AdView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, 0));
            View.AddConstraint(NSLayoutConstraint.Create(InternalView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, AdView.Bounds.Height));
            View.AddConstraint(NSLayoutConstraint.Create(InternalView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, 0));
        }
    }

    /// <summary>
    /// Shows the AdView when a new Ad loads
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void HandleAdLoaded(object sender, EventArgs e)
    {
        if (AdView == null)
            return;
        AdView.Hidden = false;
        Resize();
    }

    /// <summary>
    /// Hides the AdView when no ads are available
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void HandleFailedToReceiveAd(object sender, AdErrorEventArgs e)
    {
        if (AdView == null)
            return;
        AdView.Hidden = true;
        Resize();
    }
  }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>