IOS-9 UIStackView问题滚动到前沿
我想实现滚动的UIStackView
有两个button水平添加和删除视图。 我将scroll.contentSize
更改为scroll.contentSize
的大小,因此当堆栈大小大于ScrollView的大小时,可以滚动堆栈视图。 但是,滚动时,我无法达到堆栈视图的开始。 在滚动的时候,一开始我的观点很less,但是最后的观点可以达成。
//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; }]; } }
@ 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]; // ...
- 如何检查dispatch_async块是否已经完成运行
- 错误'缺less必要的参数:grant_type'使用Swift 3
- 如何从数据库中提取用户并使用Firebase将其列在表格视图中?
- iOS 8 – video和声音文件将不会从主屏幕的networking应用程序播放
- CoreData:警告:无法为实体“Readings”加载名为“WEEReadings”的类。 找不到类,使用默认的NSManagedObject来代替
- 将项目添加到Swift中的Firebase数组,而不先观察数组
- 担任iOS工程师30个月。 给新人的提示。
- 如何继续下载新的图像在后台,即使用户强制退出应用程序在iOS目标C?
- 为什么我的iOS应用程序不能在其他应用程序的“打开”对话框中显示?