滚动视图上的Uisegment按钮

时间:2012-12-05 10:09:42

标签: uiscrollview uisegmentedcontrol

嗨我试图在uiscrollview上放置一个uisegmented控件,因为我希望它们水平滚动这是我试过的代码

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 600, 300)];
scroll.contentSize = CGSizeMake(500, 50);
scroll.showsHorizontalScrollIndicator = YES;
scroll.pagingEnabled = YES;
scroll.showsHorizontalScrollIndicator = YES;
scroll.autoresizingMask = YES;
scroll.autoresizesSubviews = YES;
NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two",  @"Three",@"four",@"Five", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0,10, 500, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 4;

[scroll addSubview:segmentedControl];
[self.view addSubview:scroll];

任何人请帮我完成这个。在此先感谢快乐编码

1 个答案:

答案 0 :(得分:0)

滚动视图的框架必须小于滚动视图的内容大小。如果你肯定会增加你的内容大小它会起作用。例如: -

 UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    scroll.contentSize = CGSizeMake(500, 50);
    scroll.showsHorizontalScrollIndicator = YES;
    scroll.pagingEnabled = YES;
    scroll.showsHorizontalScrollIndicator = YES;
    scroll.autoresizingMask = YES;
    scroll.autoresizesSubviews = YES;
    NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two",  @"Three",@"four",@"Five", nil];


    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmentedControl.frame = CGRectMake(0,10, 500, 50);
    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
    segmentedControl.selectedSegmentIndex = 4;

    [scroll addSubview:segmentedControl];
    [self.view addSubview:scroll];

如果支持,请将答案标记为正确。非常感谢。