2

I am trying to implement scroll for UIStackView where there are two buttons to add and delete views horizontally. I am changing the scroll.contentSize as the size of UIStackView so when the stack size is greater than the ScrollView's size, stack view can be scrolled. But while scrolling I am not able to reach to the starting of stack view. Few of my views in the beginning can't be reached while scrolling but last views can be reached.

//In load view
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(25,100, screen.size.width-50, 50)];
    scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scroll];
    scroll.scrollEnabled = YES;
    scroll.delegate = self;
    cus.center = CGPointMake(scroll.frame.size.width/2, scroll.frame.size.height/2);
    [scroll addSubview:cus];

    stack = [[UIStackView alloc]init];
    stack.axis = UILayoutConstraintAxisHorizontal;
    stack.distribution = UIStackViewDistributionEqualSpacing;
    stack.spacing = 30;
    stack.alignment = UIStackViewAlignmentLeading;
    stack.translatesAutoresizingMaskIntoConstraints = NO;
    [scroll addSubview:stack];

    [stack.centerXAnchor constraintEqualToAnchor:scroll.centerXAnchor].active = YES;
    [stack.centerYAnchor constraintEqualToAnchor:scroll.centerYAnchor].active = YES;

    a = 0;//this is used to count no of views currently present
//Completion of load view

    -(void)addOn//Action performing when clicking on add button.
    {
        UIView *vampire = [[UIView alloc]init];
        vampire.backgroundColor = [UIColor blackColor];
        [vampire.widthAnchor constraintEqualToConstant:40].active = YES;
        [vampire.heightAnchor constraintEqualToConstant:40].active = YES;
        vampire.layer.cornerRadius = 20;
        a = (int)stack.subviews.count;
        [stack addArrangedSubview:vampire];
        float contentWidth = ((a-1)*30+(a*40));
        scroll.contentSize = CGSizeMake(contentWidth,vampire.frame.size.height);
    }


    -(void)removeOn//Action performing when clicking on remove button.
    {
        if(a >=0)
        {
            remove.userInteractionEnabled = NO;
        UIView *view = stack.arrangedSubviews[a];
        [UIView animateWithDuration:.2
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             view.hidden = YES;

                         } completion:^(BOOL finished) {
                              a--;
                             [view removeFromSuperview];
                             float contentWidth = ((a-1)*30+(a*40));
                             scroll.contentSize = CGSizeMake(contentWidth,view.frame.size.height);
                             remove.userInteractionEnabled = YES;
                         }];
                   }
    }

Here is the image where I can't scroll to my first views Here is the image where I can scroll to my last views

Brian
  • 14,610
  • 7
  • 35
  • 43
  • Isn't iOS 9 still under an NDA as it is still in BETA? I think it would be better if you asked these types of questions on the Apple Developer website, otherwise you will be breaking Apple's T&C's. –  Aug 04 '15 at 12:51
  • 1
    Thanks for your help I will ask this questions on the Apple Developer website. – maheswar chinnu Aug 04 '15 at 14:12
  • 2
    No, there is no NDA for information that Apple has publicly released! How can they be under NDA when all the new APIs and all WWDC sessions have been posted publicly with no login required? – lensovet Aug 05 '15 at 10:26
  • You should probably try using other constraint types instead of centerX. It looks like your stackView has some extra padding at the trailing edge, which combined with the centerX constraints, is causing your stack view to constantly center itself even when it's wider than the scroll view. – wakachamo Aug 05 '15 at 10:32
  • did you find the solution? – fabb Oct 05 '15 at 13:53

1 Answers1

0

@maheswar-chinnu, You can try ScrollableStackView : https://github.com/gurhub/ScrollableStackView

It's Objective-C and Swift compatible library. It's available through CocoaPods.

Sample Code (Swift)

import ScrollableStackView

var scrollable = ScrollableStackView(frame: view.frame)
view.addSubview(scrollable)

// add your views with 
let rectangle = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 55))
rectangle.backgroundColor = UIColor.blue
scrollable.stackView.addArrangedSubview(rectangle)
// ...

Sample Code (Objective-C)

@import ScrollableStackView

ScrollableStackView *scrollable = [[ScrollableStackView alloc] initWithFrame:self.view.frame];
scrollable.stackView.distribution = UIStackViewDistributionFillProportionally;
scrollable.stackView.alignment = UIStackViewAlignmentCenter;
scrollable.stackView.axis = UILayoutConstraintAxisVertical;
[self.view addSubview:scrollable];

UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 55)];
[rectangle setBackgroundColor:[UIColor blueColor]];

// add your views with
[scrollable.stackView addArrangedSubview:rectangle]; 
// ...
MGY
  • 7,245
  • 5
  • 41
  • 74