make an uisegmentedcontrol in an uiscrollview

那年仲夏 提交于 2020-02-02 10:36:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!