UINavigationItem.rightBarButtonItem(またはleftBarButtonItem)に複数のUIBarButtonを追加するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1414510

質問

このアプローチ/ハックを試しました: http://blog.blackwhale.at/2009/06/uibuttons-in- uinavigationbar /

問題は、これによりかすかな縫い目が残ることです。ネストされたツールバーの背景画像を、本来あるべきものをキャプチャした画像に設定してみました。それはうまくいきませんでした。画像は適用されませんでした。また、ネストされたUINavigationBarを使用しようとしましたが、動作しないようです。

これは、いくつかのiPhoneアプリで行われています。誰もが知っていますか?

[EDIT]ボタンを通常のUIBarButtonItemsのように見せ、UIBarButtonSystemItemAdd、UIBarButtonSystemItemRefreshなどのシステムスタイルを使用できるようにします。私が提供したリンクはこれを行いますが、それはナビゲーションバーにネストされたUIToolbarであるためかすかな継ぎ目を見ることができます。

これがヒューマンインターフェイスガイドラインに違反していることは言及しないでください。 (知っています)。

ハッキングに貢献してくれてありがとう...これが唯一の方法です!

役に立ちましたか?

解決

UIToolbarの背景(「シーム」)を取り除くには、UIToolbarのサブクラスを作成し、(void)drawRect:(CGRect)rectメソッドをオーバーライドします。空白のままにすると、UIToolbarの背景がなくなります。

これを自分のプロジェクトで使用しただけで、うまくいきました。これは、 http:// osmorphisのコメントにあります。 blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

他のヒント

iOS 5.0は複数のボタンをサポートするようになりました。 UINavigationItemのiOSドキュメントを参照してください。具体的には、次のとおりです。

プロパティ:

@property(nonatomic, copy) NSArray *leftBarButtonItems;
@property(nonatomic, copy) NSArray *rightBarButtonItems;
@property BOOL leftItemsSupplementBackButton;

方法:

- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;

ナビゲーションバーの右側に2つのボタンを追加するコードを投稿しました。 UIToolbar をサブクラス化する代わりに、 barStyle = -1 を設定できます。

UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myWidth, myHeight)];
// make UIView customView1... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView1];
[customView1 release];
// make UIView customView2... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView2];
[customView2 release];
UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];
[parentView release];
self.navigationItem.rightBarButtonItem = customBarButtomItem;
[customBarButtomItem release];

アップルのサイトで無料で入手できるuicatalogueの例を参照してください...彼らはuisegmentedコントロールを使用して、navigaionバーの右バーボタンの代わりに3つのボタンを表示しました...

コメントできませんが、@ iworkinprogressに加えて、UIToolbarの背景色をクリアに設定する必要がありました:

    [toolbar setBackgroundColor:[UIColor clearColor]];

これは、 httpのコメントでも見つかりました。 //osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

iOS 4.xでは、clearColorはUIToolbarに影響を与えないようですが、drawRect:をオーバーライドしました。

プロジェクト全体で使用しているヘルパー関数を思いつきました。基本的に、バーにすでにボタンがあるかどうかを確認し、新しいボタンを追加するか、既存のボタンとマージします。したがって、関数を1回または複数回呼び出すことができます。

+ (void)AddButtonToBar:(UIViewController *)controller withImage:(NSString *)imageName withAction:(SEL)action withFrame:(CGRect) frame{
    UIButton *newButton =[[UIButton alloc] init];
    [newButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    newButton.frame = frame;
    [newButton addTarget:controller action:action forControlEvents:UIControlEventTouchUpInside];

    if ([[controller.navigationItem rightBarButtonItems] count] == 0)
        [controller.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
    else {
        NSMutableArray *existingButtons = [[NSMutableArray alloc] initWithArray:[controller.navigationItem rightBarButtonItems]];
        [existingButtons addObject:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
        [controller.navigationItem setRightBarButtonItems:(NSArray *)existingButtons];
    }
}

View Controllerから呼び出します:

[Helper AddButtonToBar:self withImage:@"imageName.png" withAction:@selector(myAction) withFrame:CGRectMake(0, 0, 24, 24)];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top