Scrollview不会保留在中心指示器上

时间:2011-02-10 14:44:51

标签: iphone objective-c iphone-sdk-3.0 ios4

我随函附上我的示例代码以供参考,请将其放在中心位置,这是我遇到的问题。它不会停在中心指示器上。

注意:所有按钮都是动态的,来自网络,但在此示例中,我是从硬编码数组中创建的。但在原始场景中,他们的计数(按钮数量)可能会改变。

    - (void)viewDidLoad {

    arr = [[NSMutableArray alloc] initWithObjects:@"Test1",@"Test2",@"Test3",@"Test4",@"Test5",nil];


    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_content_slide.png"]];
    img.frame = CGRectMake(0, 20, 320, 85);
    img.contentMode = UIViewContentModeScaleAspectFill;


    [self.view addSubview:img];

    [self setScrollButtons];

    [super viewDidLoad];

}

- (void) setScrollButtons
{
    UIScrollView *tagScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 44)];
    tagScrollView.backgroundColor = [UIColor clearColor];
    [tagScrollView setShowsHorizontalScrollIndicator:NO];
    [tagScrollView setShowsVerticalScrollIndicator:NO];
    [tagScrollView setCanCancelContentTouches:NO];
    tagScrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    tagScrollView.clipsToBounds = YES;
    tagScrollView.scrollEnabled = YES;
    tagScrollView.pagingEnabled = YES;
    tagScrollView.bounces = YES;
    tagScrollView.tag = 2;

    int xaxis = 0;

    for (int i = 0; i < [arr count]; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(xaxis, 0, 160, 44);
        //btn.titleLabel.text = [NSString stringWithFormat:@"%@",[arr objectAtIndex:i]];
        [btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
        NSLog(@"%@", btn.titleLabel.text);

        xaxis += btn.frame.size.width + 20;

        [tagScrollView addSubview:btn];
    }


    [tagScrollView setContentSize:CGSizeMake([arr count] * 200, tagScrollView.frame.size.height)];

    [self.view addSubview:tagScrollView];
}

在此处查找代码UnAligned scrollview code

2 个答案:

答案 0 :(得分:0)

用这个改变你的代码

int xaxis = 0;

for (int i = 0; i < [arr count]; i++) {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(320*xaxis+80, 0, 160, 44);
    //btn.titleLabel.text = [NSString stringWithFormat:@"%@",[arr objectAtIndex:i]];
    [btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
    //[btn setBackgroundImage:[UIImage imageNamed:@"remove_fav.png"] forState:UIControlStateNormal];
    NSLog(@"%@", btn.titleLabel.text);

    xaxis ++;//= btn.frame.size.width + 40;

    [tagScrollView addSubview:btn];
}


[tagScrollView setContentSize:CGSizeMake([arr count] * 320, tagScrollView.frame.size.height)];

[self.view addSubview:tagScrollView];

它会解决你的问题

答案 1 :(得分:0)

启用分页时,如果启用分页,滚动视图会滚动到其宽度,则无法实现您想要实现的功能。为此,您可以执行的最大操作是添加此方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

CGPoint offset = scrollView.contentOffset;
int temp = floor(offset.x/160);
[tagScrollView scrollRectToVisible:CGRectMake(temp*160, 0, 320, 44) animated:YES];

}

它的配置工作正常....... {

int xaxis = 0;

for (int i = 0; i < [arr count]; i++) {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(160*xaxis+85, 0, 150, 44);
    [btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
    NSLog(@"%@", btn.titleLabel.text);
    xaxis ++;//= btn.frame.size.width + 40;
    [tagScrollView addSubview:btn];
}
   [tagScrollView setContentSize:CGSizeMake(160*xaxis+250, tagScrollView.frame.size.height)];

}

和.....

tagScrollView.pagingEnabled = NO;