I started using Xamarin because I wanted to stay in the Visual Studio 2013 environment and not have to learn a new environment. Anyway, I'm going to paste my controller's code below and hopefully somebody is smarter than me (an almost certainty) and can get me back on track.
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.
Here are the requirements for our first attempt:
- Make a UIViewController that will facilitate implementation of apple's iAD.
- Basically, we want to place an iAD banner at the bottom of the screen taking up the full width.
- We will put the view above the iAD banner and have it fill the rest of the screen.
- The banner view may disappear from time to time if no ADs are present, so we need to handle that.
We need to handle when the device rotates to accommodate new orientation.
This 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. Remember, we want to ONLY use C# in Visual Studio and not use Interface Builder at all. Here is my non working attempt:
Using the code below, I end up with the AdBanner off of the screen below the InternalView. Also, the Internal View is longer than the screen and only half the screen width. 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?
using System; using ADayBDayiOS.Views; using MonoTouch.iAd; using MonoTouch.UIKit;
namespace ADayBDayiOS { 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();
}
}
}