我開始使用Xamarin,因爲我想留在Visual Studio 2013環境中,而不必學習新的環境。無論如何,我會在下面粘貼我的控制器的代碼,希望有人比我更聰明(幾乎可以肯定),並且可以讓我回到正軌。iOS自動佈局Xamarin僅使用Visual Studio 2013中的C#代碼,無XCode或Interface Builder
我剛發現AutoLayout。在我看來,AutoLayout的理解對加速開發至關重要。但是,我沒有在Visual Studio 2013中使用純C#使用AutoLayout的許多信息。也許我只是沒有在正確的位置尋找。
無論如何,讓我們用一個簡單的控制器開始這個新的討論,該控制器完全在C#中使用AutoLayout,根本不使用任何.nib或Interface Builder的東西。而不使用Xamarin Studio。在Visual Studio 2013年
這裏只是做了所有的要求是:
- 做一個UIViewController,這將有助於實現蘋果的iAd的。
- 基本上,我們希望在屏幕底部放置一個iAD橫幅,佔據全寬。
- 我們將把視圖放在iAD橫幅的上方,讓它填充屏幕的其餘部分。
- 如果沒有AD存在,橫幅視圖可能會不時消失,因此我們需要處理該問題。
- 當設備旋轉以適應新的方向時,我們需要處理。
我們需要處理不同的設備。 iPod,iPad,iPhone,版本4和5
這應該是微不足道的,但我一直在鍵盤上敲我的頭2天試圖讓這個工作。任何建議,例子或想法都會非常有幫助。請記住,我們只想在Visual Studio中使用C#而不使用Interface Builder。這是我的非工作嘗試:
使用下面的代碼,我結束AdBanner在屏幕下面的InternalView。此外,內部視圖比屏幕更長,只有屏幕寬度的一半。這裏發生了什麼?我需要在某處打開AutoLayout功能嗎?我可以在C#代碼中執行它,還是隱藏在項目設置的某個位置?
using System;
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();
}
}
}
看一看弗蘭克的EasyLayout:http://praeclarum.org/post/ 45690317491/easy-layout-a-dsl-for-nslayoutconstraint – Krumelur
由於Xamarin實際上是AutoLayout的實現,所以我已經開始手動佈局了。我基本上在我的控制器上實現了一個'Resize'方法,當DidRotate或ViewDidAppear發生時被調用。 – Curtis