7

私はセグメント化されたコントロールを持っています。ビューの表示が終了したら、それを保持するバー ボタン アイテムを作成し、それをツールバー アイテムとして設定します。私が抱えている問題は、スペースを埋める動作が設定されていても、セグメント化されたコントロールがツールバーのスペースを埋めないことです。

iOS アプリのツールバーにスペースを埋めるセグメント化されたコントロールを配置するにはどうすればよいですか?

4

2 に答える 2

1

UIToolbar から非標準のツールバーの動作を取得しようとしているようです。そこに UIView をドロップして、通常の方法で UISegmentedControl を入力してみませんか? 必要な UIToolbar の特定の機能はありますか?

于 2012-05-04T01:51:29.590 に答える
0

一般に、UIViewには「スペースを埋める動作」はありません。割り当てられたサイズを取得します。できることは次のとおりです。

  1. 親ビューのサイズが変更された場合のサイズ変更方法を制御するために、自動サイズ変更マスクを設定します
  2. UIViewContentModeを設定して、コンテンツのサイズ変更方法を制御します(たとえば、UIImageViewsにとって重要です)。

あなたの場合、次のようにして、ツールバーと同じ幅のUISegmentedControlを含むUIToolbarを取得できます。

(void)viewDidLoad
{
    [super viewDidLoad];

    //  Create the toolbar; place it at the bottom of the view.
    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-44, self.view.bounds.size.width, 44)];
    myToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self.view addSubview:myToolbar];

    //  Create the UISegmentedControl with two segments and "Bar" style. Set frame size to that of the toolbar minus 6pt margin on both sides, since 6pt is the padding that is enforced anyway by the UIToolbar.
    UISegmentedControl *mySegmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectInset(myToolbar.frame, 6, 6)];
    //  Set autoresizing of the UISegmentedControl to stretch horizontally.
    mySegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [mySegmentedControl insertSegmentWithTitle:@"First" atIndex:0 animated:NO];
    [mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:1 animated:NO];
    mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 

    //  Create UIBarButtonItem with the UISegmentedControl as custom view, and add it to the toolbar's items
    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:mySegmentedControl];
    myToolbar.items = [NSArray arrayWithObject:myBarButtonItem];
}
于 2012-05-09T11:38:19.747 に答える