gajchtr
gajchtr

Reputation: 48

How to scroll a UIScrollView horizontally based on the buttons frame size inside the UIScrollview

Initially I need to add one default button on a scrollView.
Then, if I add one more button on the scrollView, the first default button should move to the left.
I am using this code:

int x = 0;

for (int i = 0; i < pullscount ; i++) {
    NSString *myString = [pullsarray objectAtIndex:i];
    CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:16]];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 5, 100, 40)];
    [button setTitle:[pullsarray objectAtIndex:i] forState:UIControlStateNormal];
    [button setFont:[UIFont fontWithName:@"SegoeWP-Semibold" size:15.0]];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(buttonAction:)
     forControlEvents:UIControlEventTouchUpInside];

    [button setTag:190+i];

    [topScrollView addSubview:button];

    x += button.frame.size.width+30;
}

NSLog(@"%d",x);

topScrollView.contentSize = CGSizeMake(x+200, topScrollView.frame.size.height);
topScrollView.backgroundColor = [UIColor clearColor];

In scrollViewDidScroll delegate method:

NSLog(@"%f",button.frame.size.width);    
CGFloat pageWidth = button.frame.size.width;
NSLog(@"%f",pageWidth);

if (topScrollView.contentSize.width>pageWidth) {
    float stopx = 100.0f;

    if (topScrollView.contentSize.width >= stopx) {
        CGPoint stop = topScrollView.contentOffset;

        NSLog(@"Value – %@", NSStringFromCGPoint(stop));

        stop.x = stopx;
        [topScrollView setContentOffset:stop animated:YES];
    }   
}
else if (topScrollView.contentOffset.x >= 110.0f && topScrollView.contentOffset.x <= 220.0f) {
        float stopx = 210.0f;
        CGPoint stop1 = topScrollView.contentOffset;
        stop1.x = stopx;
        [topScrollView setContentOffset:stop1 animated:YES];
        topScrollView.contentOffset = stop1;
    }
}

My requirement is that i want to scroll the UIButtons in a tableViews `headerView.
Hence I took the scrollView and placed UIButtons dynamically.

When I scroll the scrollView horizontally, it has to move horizontally as per the button's width.

I also need to be highlight the button title at certain point on scrollview when i am scrolling.

Upvotes: 0

Views: 1738

Answers (1)

staticVoidMan
staticVoidMan

Reputation: 20274

If you simply want to scroll the scrollView by 100px width then check this scenario:

- (void)viewDidLoad {
    //...
    UIButton *btnGoRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];\
    [btnGoRight setTitle:@"Scroll Right" forState:UIControlStateNormal];
    [btnGoRight addTarget:self 
                   action:@selector(scrollMyScrollView)
         forControlEvents:UIControlEventTouchUpInside];

    [btnGoRight setFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    [self.view addSubview:btnGoRight];
}

- (void)scrollMyScrollView {
    if((topScrollView.contentOffset.x + topScrollView.frame.size.width) <= topScrollView.contentSize.width) {
        CGPoint tempContentOffset = topScrollView.contentOffset;
        tempContentOffset.x += 100;  //to go right

        [topScrollView setContentOffset: tempContentOffset animated:YES];
    }
}

To go left:

Use another button (say, *btnGoLeft) and this conditional statement:

if((topScrollView.contentOffset.x + topScrollView.frame.size.width) >= 100) {
    //...
    tempContentOffset.x -= 100;  //to go left
    //...
}

Basically, we create 2 buttons (outside the scrollView) whose task is to simply scroll the scrollView to the right/left by 100px.
This is the basic concept and you can build on this to get what you're looking for


PS: Your question was vague (atleast to me) and it wasn't clear how you plan to scroll (on swipe or clicking on a button or something) or how many buttons you will have (roughly) in the scrollView so... there seemed to be multiple scenarios with what you're trying to do.
Hence, I wrote this... not as a complete answer to your question but, hopefully, to help you get on the right track.

Upvotes: 2

Related Questions