问题
I want to use a very large segmentedcontrol component so I had the idea to make it in a uiscrollview ..so by scrolling horizontally user can choose the appropriate item. I wrote this code:
CGRect rect = [[UIScreen mainScreen] applicationFrame];
CGRect frame = CGRectMake(rect.origin.x + kLeftMargin, rect.size.height - kPaletteHeight - kTopMargin, 2*rect.size.width , kPaletteHeight);
seg.frame = frame;
scroll.frame = frame;
scroll.contentSize = CGSizeMake(frame.size.width * 2,frame.size.height);
scroll.showsHorizontalScrollIndicator = YES;
scroll.showsVerticalScrollIndicator = NO;
scroll.scrollsToTop = NO;
[scroll addSubview:seg];
but the scroll view didn't let the segmentedcontrol to be seen. where was my mistake?
回答1:
Maybe this will help you:
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.selectedSegmentIndex = 1;
[scroll addSubview:segmentedControl];
[segmentedControl release];
[self.view addSubview:scroll];
回答2:
You can use UICollectionView to scroll horizontally and use buttons as cells.
来源:https://stackoverflow.com/questions/3952913/make-an-uisegmentedcontrol-in-an-uiscrollview