4

我想知道如何正确操作两个视图UISegmentController

现在我有两个UIViewsUISegmentController过程changeView

- (void)changeView:(NSInteger)index {
switch (index) {
    case 0:
        self.recipeInfoView.alpha = 1;
        self.recipeInfoView2.alpha = 0;
        break;
    case 1:
        self.recipeInfoView.alpha = 0;
        self.recipeInfoView2.alpha = 1;
        break;

    default:
        break;
}

这段代码可以工作,但是每个视图都有相同的位置和大小,使用它非常不舒服。我正在使用故事板。

4

2 回答 2

3

首先编写以下代码以创建UISegmentedControl并赋予您的UIView.

- (void)viewDidLoad
{
    [super viewDidLoad];
       
   //Make hide of your UIView 
       self.recipeInfoView.hide = YES;
       self.recipeInfoView2.hide = YES;

  //Give color of your UIView
       self.recipeInfoView.backgroundColor = [UIColor redColor];
       self.recipeInfoView.backgroundColor = [UIColor blackColor];

  //Create UISegmentedControl Controller 
    NSArray *itemArray = [NSArray arrayWithObjects: @"FirstView", @"SecondView", nil];
    self.segmentedControl= [[UISegmentedControl alloc] initWithItems:itemArray];
    self.segmentedControl.frame = CGRectMake(35, 100, 250, 33);
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    self.segmentedControl.momentary=NO;
    self.segmentedControl.tintColor=[UIColor darkGrayColor];

    [self.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.segmentedControl];
   
}

//segmentAction 方法

- (IBAction)segmentAction:(id)sender
{
    if([sender selectedSegmentIndex] == 0)
    {
       if(self.recipeInfoView.isHidden == YES)
          self.recipeInfoView.Hidden == NO;
      else
          self.recipeInfoView.Hidden == NO;

      if(self.recipeInfoView2.isHidden == YES)
           self.recipeInfoView2.Hidden == YES;
      else
           self.recipeInfoView2.Hidden == YES;

   }
   else if([sender selectedSegmentIndex] == 1)
   {
       if(self.recipeInfoView2.isHidden == YES)
           self.recipeInfoView2.Hidden == NO;
       else
           self.recipeInfoView2.Hidden == NO;

       if(self.recipeInfoView.isHidden == YES)
            self.recipeInfoView.Hidden == YES;
       else
           self.recipeInfoView.Hidden == YES; 

    }
}
于 2013-02-25T04:30:18.997 回答
1

设置视图属性 hidden = YES 或 NO 而不是为视图设置 alpha

于 2013-02-25T06:10:49.410 回答