滚动到前沿的 IOS-9 UIStackView 问题
Posted
技术标签:
【中文标题】滚动到前沿的 IOS-9 UIStackView 问题【英文标题】:IOS-9 UIStackView issue in scrolling to leading edge 【发布时间】:2015-08-04 10:04:51 【问题描述】:我正在尝试为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;
];
【问题讨论】:
ios 9 是否仍处于 NDA 之下,因为它仍处于测试阶段?我认为你最好在 Apple Developer 网站上问这些类型的问题,否则你将违反 Apple 的 T&C。 感谢您的帮助,我会在 Apple Developer 网站上提出这些问题。 不,Apple 公开发布的信息没有保密协议!当所有新 API 和所有 WWDC 会话都已公开发布且无需登录时,它们怎么可能处于 NDA 之下? 您可能应该尝试使用其他约束类型而不是 centerX。看起来您的 stackView 在后缘有一些额外的填充,与 centerX 约束相结合,导致您的堆栈视图不断居中,即使它比滚动视图更宽。 你找到解决办法了吗? 【参考方案1】:@maheswar-chinnu,你可以试试ScrollableStackView: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 问题的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UIStackView 中分隔 UITextField 前沿和训练边缘?
如何在 UIStackView (iOS 9)的中心周围均匀分布两个子视图?
Xcode 7 beta 5 中 iOS 9.0 之前的 UIStackView