iOS 6 简介Introduction to iOS 6

iOS 6 包括多种用于开发应用程序的新技术,即 iOS 6 向C#开发人员提供。iOS 6 includes a variety of new technologies for developing apps, which Xamarin.iOS 6 brings to C# developers.

使用 iOS 6 和 Xamarin iOS 6,开发人员现在可以使用丰富的功能来创建 iOS 应用程序,其中包括面向 iPhone 5 的应用程序。With iOS 6 and Xamarin.iOS 6, developers now have a wealth of capability at their disposal to create iOS applications, including ones that target iPhone 5. 本文档列出了一些更激动人心的新功能,以及每个主题的文章的链接。This document lists some of the more exciting new features that are available and links to articles for each topic. 此外,它还涉及到几项更改,这些更改在开发人员转向 iOS 6 和新的 iPhone 5 解决方案时很重要。In addition it touches on a couple changes that will be important as developers move to iOS 6 and the new resolution of iPhone 5.

集合视图简介Introduction to Collection Views

集合视图允许使用任意布局显示内容。Collection Views allow content to be displayed using arbitrary layouts. 它们允许轻松地在框中创建类似于网格的布局,同时还支持自定义布局。They allow easily creating grid-like layouts out of the box, while supporting custom layouts as well. 有关详细信息,请参阅集合视图简介指南。For more information see, the Introduction to Collection Views guide.

PassKit 简介Introduction to PassKit

PassKit 框架允许应用程序与 Passbook 应用程序中管理的数字传递交互。The PassKit framework allows applications to interact with digital passes that are managed in the Passbook app. 有关详细信息,请参阅Pass 工具包简介指南For more information see, the Introduction to Pass Kit guide.

EventKit 简介Introduction to EventKit

EventKit 框架提供了一种方法,用于访问日历数据库存储的日历、日历事件和提醒数据。The EventKit framework provides a way to access the Calendars, Calendar Events, and Reminders data that Calendar Database stores. 自 iOS 4 起,对日历和日历事件的访问已可用,但 iOS 6 现在公开对提醒数据的访问。Access to the calendars and calendar events has been available since iOS 4, but iOS 6 now exposes access to reminders data. 有关详细信息,请参阅I ntroduction to EventKit guide。For more information, see the I ntroduction to EventKit guide.

社交框架简介Introduction to the Social Framework

社交框架提供了一个统一的 API,用于与中国的用户(包括 Twitter 和 Facebook)以及 SinaWeibo 的社交网络交互。The Social Framework provides a unified API for interacting with social networks including Twitter and Facebook, as well as SinaWeibo for users in China. 有关详细信息,请参阅社交框架简介指南。For more information see, the Introduction to the Social Framework guide.

对 StoreKit 的更改Changes to StoreKit

Apple 在应用商店工具包中引入了两项新功能:从应用中购买和下载 iTunes 或应用商店内容,并托管应用内购买内容文件!。Apple has introduced two new features in Store Kit: purchasing and downloading iTunes or App Store content from within your app, and hosting your content files for in-app purchases!. 有关详细信息,请参阅应用商店工具包指南。For more information see, the Changes to Store Kit guide.

其他更改Other Changes

不推荐使用 ViewWillUnload 和 ViewDidUnloadViewWillUnload and ViewDidUnload Deprecated

UIViewControllerViewWillUnloadViewDidUnload 方法不再在 iOS 6 中调用。The ViewWillUnload and ViewDidUnload methods of UIViewController are no longer called in iOS 6. 在以前版本的 iOS 中,应用程序可能已使用这些方法在卸载视图之前保存状态,并分别清理代码。In previous versions of iOS, these methods may have been used by applications for saving state before a view unloads, and cleanup code respectively.

例如,Visual Studio for Mac 会创建一个名为 ReleaseDesignerOutlets的方法(如下所示),然后从 ViewDidUnload中调用该方法:For example, Visual Studio for Mac would create a method called ReleaseDesignerOutlets, shown below, which would then be called from ViewDidUnload:

void ReleaseDesignerOutlets ()
{
    if (myOutlet != null) {
        myOutlet.Dispose ();
        myOutlet = null;
    }
}

但是,在 iOS 6 中,不再需要调用 ReleaseDesignerOutletsHowever, in iOS 6, it is no longer necessary to call ReleaseDesignerOutlets.

对于清理代码,iOS 6 应用程序应使用 DidReceiveMemoryWarningFor cleanup code, iOS 6 applications should use DidReceiveMemoryWarning. 但是,调用 Dispose 的代码应慎用,只应用于内存密集型对象,如下所示:However, code that calls Dispose should be used sparingly and only for memory intensive objects as shown below:

if (myImageView != null){
    if (myImageView.Superview == null){
        myImageView.Dispose();
        myImageView = null;
    }
}

同样,几乎不需要调用 DisposeAgain, calling Dispose as above should rarely be needed. 通常,大多数应用程序都应该删除事件处理程序。In general the most applications should do is to remove event handlers.

对于保存状态,应用程序可以在 ViewWillDisappearViewDidDisappear 而不是 ViewWillUnload中执行此项。For the case of saving state, applications can perform this in ViewWillDisappear and ViewDidDisappear instead of ViewWillUnload.

iPhone 5 解决方案iPhone 5 Resolution

iPhone 5 设备有640x1136 的分辨率。iPhone 5 devices have a 640x1136 resolution. 当在 iPhone 5 上运行时,面向以前版本的 iOS 的应用程序将显示为 letterboxed,如下所示:Applications that targeted previous versions of iOS will appear letterboxed when run on an iPhone 5, as shown below:

为了使应用程序在 iPhone 5 上全屏显示,只需添加一个名 Default-568h@2x.png 为 "640x1136" 的映像。In order for the application to appear full-screen on iPhone 5, simply add an image named Default-568h@2x.png having a resolution of 640x1136. 下面的屏幕截图显示在包含此映像之后运行的应用程序:The following screenshot shows the application running after this image has been included:

子类 UINavigationBarSubclassing UINavigationBar

在 iOS 6 中 UINavigationBar 可以是子类。In iOS 6 UINavigationBar can be subclassed. 这允许对 UINavigationBar的外观进行更多的控制。This allows additional control of the look and feel of the UINavigationBar. 例如,应用程序可以为添加子视图提供子类,为这些视图添加动画并修改 UINavigationBar的边界。For example, applications can subclass to add subviews, animate those views and modify the Bounds of the UINavigationBar.

下面的代码演示了添加 UIImageView的子类 UINavigationBar 的示例:The code below shows an example of a subclassed UINavigationBar that adds a UIImageView:

public class CustomNavBar : UINavigationBar
{
    UIImageView iv;
    public CustomNavBar (IntPtr h) : base(h)
    {
        iv = new UIImageView (UIImage.FromFile ("monkey.png"));
        iv.Frame = new CGRect (75, 0, 30, 39);
    }
    public override void Draw (RectangleF rect)
    {
        base.Draw (rect);
        TintColor = UIColor.Purple;
        AddSubview (iv);
    }
}

若要将子类 UINavigationBar 添加到 UINavigationController,请使用采用 UINavigationBar 类型和 UIToolbarUINavigationController 构造函数,如下所示:To add a subclassed UINavigationBar to a UINavigationController, use the UINavigationController constructor that takes the type of the UINavigationBar and UIToolbar, as shown below:

navController = new UINavigationController (typeof(CustomNavBar), typeof(UIToolbar));

使用此 UINavigationBar 子类将导致显示图像视图,如以下屏幕截图所示:Using this UINavigationBar subclass results in the image view being displayed as shown in the following screenshot:

界面方向Interface Orientation

在 iOS 6 应用程序可以重写 ShouldAutorotateToInterfaceOrientation的情况下,为特定控制器支持的任何方向返回 true。Prior to iOS 6 applications could override ShouldAutorotateToInterfaceOrientation, returning true for any orientations the particular controller supported. 例如,以下代码将仅用于支持纵向:For example, the following code would be used to support only portrait:

public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
    {
        return (toInterfaceOrientation == UIInterfaceOrientation.Portrait);
    }

在 iOS 6 中 ShouldAutorotateToInterfaceOrientation 弃用。In iOS 6 ShouldAutorotateToInterfaceOrientation is deprecated. 相反,应用程序可以在根视图控制器上重写 GetSupportedInterfaceOrientations,如下所示:Instead applications can override GetSupportedInterfaceOrientations on the root view controller as shown below:

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations ()
    {
        return UIInterfaceOrientationMask.Portrait;
    }

在 iPad 上,如果未实现 GetSupportedInterfaceOrientation,则默认为四个方向。On iPad, this defaults to all four orientations if GetSupportedInterfaceOrientation is not implemented. 在 iPhone 和 iPod touch 上,默认值为除 PortraitUpsideDown之外的所有方向。On iPhone and iPod Touch, the default is all orientations except PortraitUpsideDown.