98

ツールバーにラベルを追加しようとしています。ボタンはうまく機能しますが、ラベル オブジェクトを追加するとクラッシュします。何か案は?

UIBarButtonItem *setDateRangeButton = [[UIBarButtonItem alloc] initWithTitle:@"Set date range"
                                                                       style:UIBarButtonItemStyleBordered
                                                                      target:self
                                                                      action:@selector(setDateRangeClicked:)];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
label.text = @"test";

[toolbar setItems:[NSArray arrayWithObjects:setDateRangeButton,label, nil]];

// Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];

// Reload the table view
[self.tableView reloadData];
4

10 に答える 10

129

これを見てください

[[UIBarButtonItem alloc] initWithCustomView:yourCustomView];

基本的にすべてのアイテムは「ボタン」でなければなりませんが、必要なビューでインスタンス化できます。ここにいくつかのコード例があります。通常、他のボタンはツールバーにあるため、タイトル ボタンの両側にスペーサーが配置され、中央に配置されることに注意してください。

NSMutableArray *items = [[self.toolbar items] mutableCopy];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer];
[spacer release];

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
[self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]];
[self.titleLabel setText:@"Title"];
[self.titleLabel setTextAlignment:NSTextAlignmentCenter];

UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer2];
[spacer2 release];

UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:self.titleLabel];
[items addObject:title];
[title release];

[self.toolbar setItems:items animated:YES];
[items release];
于 2008-12-02T09:59:34.443 に答える
121

Interface Builderを使用してレイアウトするUIToolBar場合は、InterfaceBuilderのみを使用してこれを行うこともできます。

にを追加するUILabelには、新しいオブジェクトをにドラッグして、IBに汎用オブジェクトをUIToolBar追加する必要があります。カスタムで初期化されるを自動的に作成します。次に、にを追加し、好みのスタイルに一致するようにグラフィカルに編集します。次に、必要に応じて固定スペーサーや可変スペーサーを視覚的に設定して、適切な位置に配置できます。UIViewUIToolBarUIViewUIToolBarIBUIBarButtonItemUIViewUILabelUIViewUILabelUILabel

また、との下に正しく表示されるようにするには、UILabelとの両方の背景を設定する必要があります。UIViewclearColorUIToolBarUILabel

于 2011-01-07T21:15:50.630 に答える
6

私がこのトリックを使用していることの 1 つは、 のUIActivityIndicatorView上にをインスタンス化することです。これは、他のUIToolBar方法では不可能なことです。たとえば、ここでは a UIToolBarwith 2 UIBarButtonItem、 a FlexibleSpaceBarButtonItem、そして別の がありUIBarButtonItemます。フレキシブル スペースと最後の (右側の) ボタンUIActivityIndicatorViewの間にa を挿入したいと思います。UIToolBarだから私の中で私RootViewControllerは次のことをします、

- (void)viewDidLoad {
[super viewDidLoad];// Add an invisible UIActivityViewIndicator to the toolbar
UIToolbar *toolbar = (UIToolbar *)[self.view viewWithTag:767];
NSArray *items = [toolbar items];

activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 20.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];    

NSArray *newItems = [NSArray arrayWithObjects:[items objectAtIndex:0],[items objectAtIndex:1],[items objectAtIndex:2],
                     [[UIBarButtonItem alloc] initWithCustomView:activityIndicator], [items objectAtIndex:3],nil];
[toolbar setItems:newItems];}
于 2009-02-07T02:36:09.200 に答える
2

Matt RI と同様に、インターフェイス ビルダーを使用しました。UIWebViewしかし、一部のテキストを太字にし、他のテキストを太字にできないようにするために、代わりに1つ入れたいと思いました(メールアプリのように)。そう

  1. 代わりに webview を追加してください。
  2. 不透明のチェックを外す
  3. 背景がクリアな色であることを確認してください
  4. すべてを接続するIBOutlet
  5. 以下htmlを使用して背景を透明にし、ツールバーが透けて見えるようにします

コード:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *html = [NSString stringWithFormat:@"<html><head><style>body{font-size:11px;text-align:center;background-color:transparent;color:#fff;font-family:helvetica;vertical-align:middle;</style> </head><body><b>Updated</b> 10/11/12 <b>11:09</b> AM</body></html>"];
[myWebView loadHTMLString:html baseURL:baseURL];
于 2012-10-11T15:51:10.117 に答える
1

これを試して:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(140 , 0, 50, 250)];
[label setBackgroundColor:[UIColor clearColor]];
label.text = @"TEXT";
UIView *view = (UIView *) label;
[self.barItem setCustomView:view];

注:self.barItemUIBarButtonItemオブジェクト ライブラリから追加され、2 つの柔軟なスペースの間に配置されます。

もう 1 つの方法は、線を削除し[self.barItem setCustom:view]て (幅) のパラメーターを変更し、labelツールバー全体を塗りつぶし、配置を中央に設定し、フォントをコードで自分で設定することです。

于 2013-03-08T16:46:17.497 に答える
1

ツールバービューにビューを追加したい場合は、これを試すことができます:

[self.navigationController.tabBarController.view addSubview:yourView];
于 2013-07-04T10:13:54.597 に答える