0

嗨,我试图在 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];

任何人请帮我完成这个。提前感谢快乐编码

4

1 回答 1

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];

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

于 2013-06-06T11:09:21.050 回答