UIStackView
ビューを水平に追加および削除するための2つのボタンがある場所にスクロールを実装しようとしています。スタックサイズがScrollViewのサイズよりも大きい場合、スタックビューをスクロールできるようにscroll.contentSize
サイズを変更しています。UIStackView
しかし、スクロール中にスタック ビューの先頭に到達できません。スクロール中に最初のビューのいくつかに到達できませんが、最後のビューに到達できます。
//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;
}];
}
}