【发布时间】: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 约束相结合,导致您的堆栈视图不断居中,即使它比滚动视图更宽。
-
你找到解决办法了吗?
标签: ios objective-c ios9 uistackview