倒叙输出addSubView(最先添加的放最后显示,显示在底部)
效果图:
int tempX = 0;
NSMutableArray * buttons = @[].mutableCopy;
for (NSInteger i = 0; i < MaxShowViewer; i++) {
UIButton * button = [UIButton new];
button.tag = BaseButtonTag + i;
button.backgroundColor = kRANDOM_COLOR;
[button addTarget: self action: @selector(showViwerDetail: ) forControlEvents: UIControlEventTouchUpInside];
button.frame = CGRectMake(tempX, 0, 30, 30);
STViewBorderRadius(button, 15, 0, STClearColor);
[buttons addObject: button];
tempX += 25;
}
[buttons enumerateObjectsWithOptions: NSEnumerationReverse usingBlock: ^ (id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.audienceScrollView addSubview: buttons[idx]];
}];
CGSize size = self.audienceScrollView.contentSize;
size.width = tempX;
//设置不显示横拉动条
self.audienceScrollView.showsHorizontalScrollIndicator = NO;
//showsVerticalScrollIndicator 设置竖滚动条是否显示
self.audienceScrollView.contentSize = size;
//设置反弹效果
self.audienceScrollView.bounces = YES;
关键代码
[buttons enumerateObjectsWithOptions: NSEnumerationReverse usingBlock: ^ (id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.audienceScrollView addSubview: buttons[idx]];
}];