2

我正在尝试UIStackView在有两个按钮水平添加和删除视图的位置实现滚动。我正在更改scroll.contentSize大小,UIStackView因此当堆栈大小大于 ScrollView 的大小时,可以滚动堆栈视图。但是在滚动时我无法到达堆栈视图的开头。滚动时无法到达我最初的几个视图,但可以到达最后一个视图。

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

这是我无法滚动到我的第一个视图的图像 这是我可以滚动到最后一个视图的图像

4

1 回答 1

0

@maheswar-chinnu,你可以试试ScrollableStackViewhttps ://github.com/gurhub/ScrollableStackView

它是 Objective-C 和 Swift 兼容的库。它可以通过CocoaPods 获得

示例代码(斯威夫特)

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)
// ...

示例代码(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]; 
// ...
于 2016-12-20T20:03:13.267 回答