67

のフレームをどのように定義しUISegmentedControlますか? container viewセグメント化されたコントロールをieの下部に表示したいと思いUIViewます。

4

8 に答える 8

182

これは私がテストした完璧なものです.....

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)];
scroll.contentSize = CGSizeMake(320, 700);
scroll.showsHorizontalScrollIndicator = YES;

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 1;     
[scroll addSubview:segmentedControl];
[segmentedControl release]; 
[self.view addSubview:scroll];

次に、クラスにメソッドを追加します。

- (void)MySegmentControlAction:(UISegmentedControl *)segment 
{    
    if(segment.selectedSegmentIndex == 0)
    {
        // code for the first button
    } 
}

非推奨のUISegmentedControlStyleについては、この URLを確認できます。

于 2011-07-14T07:03:48.513 に答える
21

あなたはこのようにすることができます...

UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];

[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentControl.frame = CGRectMake(10, 50, 300, 30);
[segmentControl addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[segmentControl setSelectedSegmentIndex:0];
[scrollView addSubview:segmentControl];
[segmentControl release];

ステップ2:

-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
    case 0:{
        //action for the first button (Current)
        break;}
    case 1:{
        //action for the first button (Current)
        break;}
    }
}
于 2013-11-16T05:52:43.273 に答える
7

手順 1.インデックス値を使用してセグメント コントロールを作成する

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common-bg.jpg"]];
    [self.navigationItem setHidesBackButton:YES];

    //-- For creating segment control in navigation bar
     UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", @"Month", @"Year", @"Home", nil]];
    [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    mainSegment.frame = CGRectMake(0,0, 400, 43);
    self.navigationItem.titleView = mainSegment;
    mainSegment.selectedSegmentIndex = 1;
    [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged];
    [self.view addSubview:mainSegment];
    //--**--

}

ステップ 2.サブビューを作成する

- (void)mainSegmentControl:(UISegmentedControl *)segment
{

    if(segment.selectedSegmentIndex == 0)
    {
        // action for the first button (Current or Default)
    }
    else if(segment.selectedSegmentIndex == 1)
    {
        // action for the second button 
    }
    else if(segment.selectedSegmentIndex == 2)
    {
        // action for the third button 
    } 
    else if(segment.selectedSegmentIndex == 3)
    {
        // action for the fourth button 
    }
}
于 2013-11-08T09:40:11.993 に答える
2

これは、すべてのタイプの iOS デバイスで機能します。

UISegment *segment = [[UISegmentedControl alloc] initWithItems:array];
segment.frame = CGRectMake(0, self.view.frame.size.height - 40, 300, 40);
UIFont *font = [UIFont fontWithName:@"DroidSans" size:18.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                       forKey:NSFontAttributeName];
[segment setTitleTextAttributes:attributes
                                forState:UIControlStateNormal];
[segment addTarget:self action:@selector(segmentAction:) forControlEvents: UIControlEventValueChanged];
[self.view addSubview:segment];
于 2015-08-17T05:37:57.643 に答える