开发者

Scrollview doesn't remains on the center indicator

I am enclosing my sample code herewith for ready reference, please make it in center place, this is what I am facing problem in. It is not stopping on the center indicator.

Note : All button are dynamic and coming from web, but in this sample I 开发者_如何学Cmade it from a hard coded array. But in original scenerio their count(number of buttons) may change.

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

Find code here UnAligned scrollview code


chage ur code with this one

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

it will solve ur problem


What you want to achieve is not possible when pagination is enabled as when pagination is enabled the scroll view scroll to its width.So for this what you can do max is add this method

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

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

}

its working fine for this configuration ....... {

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

}

and .....

tagScrollView.pagingEnabled = NO;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜