gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:30:37 29 4
gpt4 key购买 nike

我正在尝试为 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com