1

カスタム コントロールで UINavigationBar を実装しようとしています。そのため、UINavigationBar の左側に UIView を追加し、次のコードでその UIView にコントロールを追加しようとしました。

UIView *view = navigationController.navigationItem.leftBarButtonItem.customView;
UIButton *button = [[UIButton alloc] initWithFrame:view.frame];
[button setTitle:@"TEST" forState:UIControlStateNormal];
[view addSubview:button];
[button sizeToFit];
[button release];

コードは機能しますが、ボタンは表示されません。

どんな助けでも大歓迎です。ありがとうございました。

UPD

OK、私は以下のコードを試してみて、そのようなことをしました:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 50)];
[button setTitle:@"XXX" forState:UIControlStateNormal];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationController.visibleViewController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
[button release];

「XXX」というタイトルが出ていましたが、ボタンではなく単なるラベルのようです。何か案は?

4

2 に答える 2

4

はい、あなたの UIButton にはシェーディングがありません。私が行ったことは、UISegmentedControl を使用してシェーディングを無料で取得することです。

// Add the Menu button to the navigation bar
NSString* menuLabel = "ShadeButton";
CGSize textSize = [menuLabel sizeWithFont:[UIFont systemFontOfSize:15.0]];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc]
    initWithFrame:CGRectMake(0.0f, 0.0f, textSize.width, 29.0f)];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton insertSegmentWithTitle:menuLabel atIndex:0
                              animated:NO];
[menuSegmentedButton addTarget:self action:@selector(doMenu)
         forControlEvents:UIControlEventValueChanged];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] 
    initWithCustomView:menuSegmentedButton];
[menuSegmentedButton release];
self.navigationItem.rightBarButtonItem = barButton;

上記のように、UIButton ではなく UISegmentedControl を使用して UIBarButtonItem を作成すると、目的の効果が得られるはずです。もう 1 つの方法は、ボタンにさらに作業を加えて、独自のテクスチャ/カスタム イメージを作成することです。

于 2010-03-05T02:49:43.170 に答える
3

かなり正しいことに注意してください。

別のタイトルが必要な場合:

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"TEST" style:UIBarButtonItemStylePlain target:nil action:nil];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];

カスタムビューが本当に必要な場合:

// Create Custom View called myView.
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
于 2010-02-27T22:44:54.770 に答える