問題は、ドキュメントの準備ができるとボタンが再生成されたように見えることです。「準備完了」を実際に定義することはできませんが、テストアプリを作成し、いくつかのことに気づきました。
- NavigationItemの右バーボタンの設定はiOS6では機能しません
- ビュー階層をスキャンしてUIToolbarのインスタンスを検索し、ツールバーのボタンを設定すると機能します。
上記のハックは、表示されているドキュメントが十分に「準備ができている」場合にのみ機能します。大きなドキュメントは時間がかかります。私は2段階の解決策を思いついた:
- ナビゲーションアイテムを検索する
- NSTimerを使用して常に非表示にします。
ここにいくつかのコードがあります:
これはViewDidAppear()からのものです:
ボタンを隠し続けるタイマーを設定します。
NSTimer oTimer = NSTimer.CreateRepeatingTimer(0.2, this.HidePrintButton);
NSRunLoop.Current.AddTimer(oTimer, NSRunLoopMode.Default);
private void HidePrintButton()
{
if(this.oNavItem == null)
{
return;
}
this.InvokeOnMainThread(
delegate {
this.oNavItem.SetRightBarButtonItems( new UIBarButtonItem[0], false );
} );
}
これにより、ナビゲーションアイテムが検索されます。
/// <summary>
/// Finds the first navigation item inside a view hierachy.
/// </summary>
/// <param name='oCurrentView'>the view to start searching from</param>
/// <param name='oItem'>will be set if the navigation item was found</param>
public static void FindNavigationItem(UIView oCurrentView, ref UINavigationItem oItem)
{
if(oItem != null || oCurrentView == null || oCurrentView.Subviews == null || oCurrentView.Subviews.Length <= 0)
{
return;
}
// Check if a UINavigationBar was found. This will contain the UINavigationItem.
if(oCurrentView is UINavigationBar)
{
UINavigationBar oBar = (UINavigationBar)oCurrentView;
if(oBar.Items != null && oBar.Items.Length > 0)
{
oItem = oBar.Items[0];
}
return;
}
// Recursively loop all sub views and keep on searching.
foreach (var oSubView in oCurrentView.Subviews)
{
FindNavigationItem(oSubView, ref oItem);
if(oItem != null)
{
break;
}
}
}