3

UINavigationItem にボタンを追加する必要がありますtitleView(実際には UINavigation バーの中央に)。

これを達成するために次のコードを使用しました。

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 70, 30);
[btn setSelected:YES];
[btn setTitle:@"list" forState:UIControlStateNormal];

そして、私は次のようにしました:

ここに画像の説明を入力

UIBarButtonItemボタン「リスト」に「完了」ボタン(これは)と同じスタイルを与える必要がありますか?

4

4 に答える 4

7

UISegmentedControlを使用できます。

UISegmentedControl *mySegmentedControl = [[UISegmentedControl alloc]
                initWithItems:@"Example"]];
mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[mySegmentedControl addTarget:self action:@selector(myMethod)
       forControlEvents:UIControlEventValueChanged];

self.navigationItem.titleView = mySegmentedControl;
于 2012-11-22T23:00:59.497 に答える
1

UIBarButtonItems は UIToolbar のアイテムとしてのみ使用できます (実際には UIView のインスタンスではありません)。したがって、titleView で UIBarButtonItem を直接使用する唯一の方法は、UIToolbar のインスタンスを titleView として設定し、barButtonItem をそれに追加することです。ツールバー。しかし、私はそれがうまくいくとは思わない。

あなたがする必要があるのは、プレーンな古い UIButton を使用して UIBarButtonItem スタイルを模倣する方法を見つけることだと思います。UIKit artwork extractorを使用して、UIBarButtonItems の作成に使用される実際の画像ファイルを取得できます。次に、その背景画像を使用してカスタム UIButton を作成するだけで、準備完了です。

于 2012-02-15T19:16:30.027 に答える
1

私は andrej の UISegmentedControl ベースのアプローチを使用し、それを少し拡張して、通常の UIButton のように動作させました。

@interface SPWKBarButton : UISegmentedControl
- (id)initWithTitle:(NSString *)title;
- (void)setTitle:(NSString *)title;
@end

@implementation SPWKBarButton 

- (id)initWithTitle:(NSString *)title
{
    self = [super initWithItems:@[title]];

    if (self) {
        self.segmentedControlStyle = UISegmentedControlStyleBar;

        NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                               forKey:UITextAttributeTextColor];

        [self setTitleTextAttributes:attributes
                            forState:UIControlStateNormal];
    }

    return self;
}

- (void)setTitle:(NSString *)title
{
    [self setTitle:title forSegmentAtIndex:0];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) {
        self.selectedSegmentIndex = 0;
    } else {
        self.selectedSegmentIndex = UISegmentedControlNoSegment;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    }

    self.selectedSegmentIndex = UISegmentedControlNoSegment;
}

@end

基本的な使い方として、UIButton ドロップインの代わりとして使用できます。

_button = [[SPWKBarButton alloc] initWithTitle:titles[0]];

[_button addTarget:self
            action:@selector(doSomething:)
  forControlEvents:UIControlEventTouchUpInside];

[self.navigationItem setTitleView:_button];

このイベントは、セグメント化されたコントロールの境界内でタッチアップが発生した場合にのみ発生します。楽しみ。

于 2013-05-28T12:51:32.523 に答える
0

はい、ボタン「リスト」にも UIBarButtonItem を使用する必要があります

カスタム識別子とタイトル プロパティを使用します。

UIBarButtonItem 識別子「フレキシブル スペース」を使用して、ボタンの「リスト」を中央に表示できます。

于 2012-02-15T19:16:22.980 に答える