问题描述
- `UISegmentController`和`UIViews`
-
我想知道通过UISegmentController怎么操作两个view。现在有两个UIView和UISegmentController。
- (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; }
代码可以运行,但是每个view都在同一个位置,尺寸也一样。用起来不方便。
我用的storyboard。
解决方案
用下面的代码创建UISegmentedControl,并且给UIColor上色。
- (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 Methods
- (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;
}
}
时间: 2024-10-29 10:18:41