2

右のバーボタンに2つのボタンを含めたいと思いました。そこで、UIToolbarを使用してそれを行いました。しかし、問題は、2つのボタンが互いに離れて配置されていることです。一方、私が達成したい効果は、ボタンが互いに面一になっていることです。

これが彼らが今どのように見えるかの画像です

ここに画像の説明を入力してください

これまでにボタンを実現するために使用したコードは次のとおりです

    UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 2.0f, 120.0f, 40.01f)]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = YES;
tools.tintColor = [UIColor blueColor];
tools.barStyle = -1;// -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];


UIButton * upButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
upButton.frame = CGRectMake(0, 07, 46, 30);
upButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:16];
[upButton setTitle:@"" forState:UIControlStateNormal];
upButton.backgroundColor = [UIColor clearColor];
[upButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[upButton addTarget:self action:@selector(pageDown) forControlEvents:UIControlEventTouchUpInside];
[upButton setBackgroundImage:[UIImage imageNamed:@"page_up.png"] forState:UIControlStateNormal];
[upButton setBackgroundImage:[UIImage imageNamed:@"page_up_action.png"] forState:UIControlStateSelected];
UIBarButtonItem *toggleSegmentedControlBarItemOne = [[UIBarButtonItem alloc] initWithCustomView:upButton];
[buttons addObject:toggleSegmentedControlBarItemOne];

UIButton * downButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
downButton.frame = CGRectMake(0, 07, 46, 30);
downButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:16];
[downButton setTitle:@"" forState:UIControlStateNormal];
downButton.backgroundColor = [UIColor clearColor];
[downButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[downButton addTarget:self action:@selector(pageUp) forControlEvents:UIControlEventTouchUpInside];
[downButton setBackgroundImage:[UIImage imageNamed:@"page_down.png"] forState:UIControlStateNormal];
[downButton setBackgroundImage:[UIImage imageNamed:@"page_down_action.png"] forState:UIControlStateSelected];
UIBarButtonItem *toggleSegmentedControlBarItemTwo = [[UIBarButtonItem alloc] initWithCustomView:downButton];
[buttons addObject:toggleSegmentedControlBarItemTwo];

[tools setItems:buttons animated:NO];

[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];

誰かがこれらの2つのボタンを隙間なく並べて配置する方法を教えてもらえますか?

どうもありがとう、-コード

4

3 に答える 3

2

UISegmentedControlをcustomViewとしてnavigationItemに追加し、そのモーメンタリプロパティをtrueに設定すると、必要なものを正確に取得できます。

これがサンプルコードです

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.title = @"Test";

    UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
    segControl.momentary = YES;
    segControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segControl.tintColor = [UIColor darkGrayColor];

    [segControl insertSegmentWithImage:[UIImage imageNamed:@"up.png"] atIndex:0 animated:NO];
    [segControl insertSegmentWithImage:[UIImage imageNamed:@"down.png"] atIndex:1 animated:NO];

    [segControl addTarget:self action:@selector(segButtonDown:) forControlEvents:UIControlEventValueChanged];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segControl];

}

- (void)segButtonDown:(id)sender {

    UISegmentedControl *segControl = (UISegmentedControl *)sender;

    switch (segControl.selectedSegmentIndex) {
        case 0:
            NSLog(@"Up");
            break;
        case 1:
            NSLog(@"Down");
        default:
            break;
    }

}

そして、これがどのように見えるかの画像です。

スクリーンショット

そして、ここではコンソールでイベントを見ることができます。

2012-11-25 16:03:47.805 test2[13886:c07] Up
2012-11-25 16:03:48.367 test2[13886:c07] Down
2012-11-25 16:03:48.930 test2[13886:c07] Up
2012-11-25 16:03:49.538 test2[13886:c07] Down
于 2012-11-25T14:46:02.507 に答える
0

2つのボタンの間にスペースを追加しません。2つはこれを行います。

UIBarButtonItem * noSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease]; noSpace.width = -10.0;

このnoSpaceオブジェクトを配列(この場合はボタン)に挿入します。UIBarButtonItemの2つのオブジェクト間。

大丈夫だよ。

于 2013-02-15T05:25:34.547 に答える
0

このチュートリアルを見てください、多分それは役に立ちます:

Xcode:ナビゲーションバーの複数のボタン

于 2012-11-25T15:16:30.660 に答える