ios使用for循环根据个数居中创建一排按钮的代码实现
首先根据数组中的元素个数计算这排ui的其实点x,其中kScreenWidth/2是屏幕中间,currentHandler.iconCount是数组,padding是控件之间的间隔,然后遍历数组创建控件时从计算好的起点开始累加每个控件的宽度和间隔就好了
代码如下:
CGFloat beginX=kScreenWidth/2- (currentHandler.iconCount*30+(padding*currentHandler.iconCount-1))/2;
for(NSInteger i=0;i<currentHandler.iconCount;i++)
{
UIImageView *iv=[[UIImageViewalloc]initWithFrame:CGRectMake(beginX+i*(30+padding),CGRectGetMaxY(rewardInfoLabel.frame)+8,30, 30)];
[iv setImageWithURL:[NSURLURLWithString:[currentHandler.iconArrobjectAtIndex:i]] placeholderImage:[UIImageimageNamed:@"t-02"]];
[rewardView addSubview:iv];
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: NSArray不用遍历能否判断一个对象是否存在在数组里面?
- 下一篇: iOS数组倒序排列