滚动到前沿的 IOS-9 UIStackView 问题

标签 ios objective-c ios9 uistackview

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

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

最佳答案

@maheswar-chinnu,你可以试试可滚动堆栈 View : https://github.com/gurhub/ScrollableStackView

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

示例代码 (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)
// ...

示例代码(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]; 
// ...

关于滚动到前沿的 IOS-9 UIStackView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31806320/

相关文章:

uikit - AutomaticAdjustsScrollViewInsets 在 iOS9 中不起作用

iphone - 关闭模态视图时在 IOS 5 中崩溃

ios9 - 如何查看应用程序当前运行在哪个屏幕?

ios - 将旧的 firebase 代码转换为最新版本的问题

ios - 无法在循环中切换 Bool

iphone - Dropbox 关闭 modalview Controller 不工作

objective-c - ARC : property vs ivar: copy, 弱

ios - SpriteKit 中未呈现的场景

ios - 如何在 Swift 中以编程方式为 UILabel 提供动态高度?

ios - 使 uitableview 的行高适合他的内容