-2

画面の下部にあるツールバーに UIButton を追加するのに苦労しています。問題は、ツールバーの上に表示されず、その下に隠れていることです。

追加しようとしているツールバーのコード (および以下の [戻る] ボタンのコード) は次のとおりです。

座標は 100% ではないので無視してください。ただし、少なくとも iPhone バージョンは同じ領域の周りにあるため、ツールバーに表示される必要があります。後で正確に調整できます。

//Insert Main Toolbar On Main View
    if (IS_IPHONE_5) {

            //offsetY = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 6 : 6 * 2;

        offsetY = (1136 - 960) / 2;

        frameRect = CGRectMake(0, SCREEN_HEIGHT - 53, 320 * 2, 53);

    }else if (IS_IPHONE || IS_IPHONE_4) {
            offsetY = 0;
            frameRect = CGRectMake(0, SCREEN_HEIGHT - 53, 320 * 2, 53);

    } else {

        frameRect = CGRectMake(0, 1024 - 53 * 2, 768 * 2, 53 * 2);
    }

  offsetY = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 6 : 6 * 2;

    main_toolbar = [[UIImageView alloc] initWithFrame:frameRect];
    [main_toolbar setImage:[UIImage imageNamed:[g_AppUtils resourceNameForDevice:@"ToolbarBackground" ofType:@"png"]]];
    main_toolbar.userInteractionEnabled = YES;
    [self.view addSubview:main_toolbar];
    [main_toolbar release];

    //Insert Go Back Button On Main Toolbar
    goHome_button = [[UIButton alloc] initWithFrame:frameRect];
    goHome_button = [UIButton buttonWithType:UIButtonTypeCustom];
    [goHome_button setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];

    [goHome_button addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        goHome_button.frame = CGRectMake(55, 410, 46, 42);
    } else {
        goHome_button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    }

   [main_toolbar addSubview:goHome_button];
  [self.view addSubview:goHome_button];
4

2 に答える 2

2

UIButton を関連付けるには、initWithCustomView を使用して UIBarButtonItem を作成する必要があります。次に、すべてのバー ボタン アイテムを UIToolBar の setItems メソッドに追加します。このようなもの:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[buttons addObject:barButton];  // buttons is a NSMutableArray
[mainToolBar setItems:buttons animated:YES];  

間隔について:

UIBarButtonItem *spacerFlex = [[UIBarButtonItem alloc]
                            initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                               target:nil
                               action:nil];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                               target:nil
                               action:nil];
[spacer setWidth:50];  // 50 just an example

ボタン配列に spcerFlex またはスペーサーを追加します...必要なレイアウトごとに。

于 2013-04-02T03:40:44.010 に答える
1

ビューにボタンを追加しています。

...  
[main_toolbar addSubview:goHome_button];
[self.view addSubview:goHome_button]; <--- Remove this line
于 2013-04-02T00:47:37.747 に答える