我必须像这样设计屏幕。基本类别包含4个项目,然后厨房类别包含8个项目,然后实用程序类别包含11个项目。如何使用可重用性?
这是我试过的我的代码:
- (void)buildAppliancesView
{
for (int i=0; i<5; i++)
{
[self addAppliancesCategoriesLabel:i];
[self addApplianceCategoryView:i];
[self addIndividualAppliances:i];
}
}
- (void)addAppliancesCategoriesLabel:(int)y
{
label = [[UILabel alloc] initWithFrame:CGRectMake(0,(_appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height)+y*150+10, 0, 0)];
NSLog(@"y=>%f",label.frame.origin.y);
label.text =[NSString stringWithFormat:@"%@",[APPLIANCESCATEGORIESLABEL_ARRAY objectAtIndex:y]];
[label sizeToFit];
[_scrollView addSubview:label];
}
- (void)addApplianceCategoryView:(int)y
{
applianceCategoryView = [[UIView alloc]initWithFrame:CGRectMake(5, label.frame.origin.y+label.frame.size.height+5, self.view.frame.size.width-10, 70)];
[applianceCategoryView setBackgroundColor:[UIColor grayColor]];
[_scrollView addSubview:applianceCategoryView];
}
- (void)addIndividualAppliances:(int)y
{
for (int i=0; i<4; i++)
{
UIView *applianceView = [[UIView alloc]initWithFrame:CGRectMake(5+((self.view.frame.size.width/4)+5)*y, _appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height+label.frame.size.height+10, 50, 50)];
NSLog(@"xVAlue==>>%f",applianceView.frame.origin.x);
[applianceView setBackgroundColor:[UIColor redColor]];
UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changecolor)];
[applianceView addGestureRecognizer:singleTap];
[applianceCategoryView addSubview:applianceView];
}
}
答案 0 :(得分:0)
选中此项 - 用于在循环中创建视图的示例代码。
int xPosition = 0;
int yPosition = 0;//set your y origin
int height = 150; // sample value
int width = (NumberOfItems.count)/4;
for (int i = 0; i < NumberOfItems.count; i++) {
UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, yPosition, width, height)];
[self.scrollView addSubview:view];
xPosition += width;
if(xPosition == 3*width){
xPosition=0;
yPosition = yPosition + height; // set as per your requirement
}
}
使用UIScrollview会更好。
OR
以下是您的方案的示例代码。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapDetected:)];
// singleTap.numberOfTapsRequired=1;
UIScrollView *ScrollVc = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
ScrollVc.delegate = self;
ScrollVc.showsHorizontalScrollIndicator = NO;
[ScrollVc setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:ScrollVc];
ScrollVc.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// [ScrollVc addGestureRecognizer:singleTap];
NSArray *essentialsArr = @[@"1",@"2",@"3"];
NSArray *KitchenArr = @[@"1",@"2",@"3",@"4",@"5",@"6"];
NSArray *abcArr = @[@"1",@"2",@"3",@"4"];
NSArray *arr = [NSArray arrayWithObjects:essentialsArr,KitchenArr,abcArr, nil];
int yPosition = 0;//set your y origin
int yPositionOfMain = 0;//set your y origin
for (int k = 0; k < arr.count; k++) {
int xPosition = 0;
int height = 100; // sample value
int width = ([[UIScreen mainScreen] bounds].size.width)/4;
UIView *mainview = [[UIView alloc] init];
mainview.backgroundColor = [UIColor yellowColor];
[ScrollVc addSubview:mainview];
for (int i = 0; i < [[arr objectAtIndex:k] count]; i++) {
if (i==0) {
xPosition=0;
yPosition= 0;
}
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(xPosition + 10, yPosition, width-20, height)];
view.backgroundColor = [UIColor redColor];
[mainview addSubview:view];
xPosition += width;
if(xPosition == 4*width && i!=[[arr objectAtIndex:k] count]-1){
xPosition=0;
yPosition = yPosition + height + 10; // set as per your requirement
}
}
mainview.frame = CGRectMake(0, yPositionOfMain, [[UIScreen mainScreen] bounds].size.width, yPosition + height + 10);
yPositionOfMain = yPositionOfMain + mainview.frame.size.height + 20;
}
if ([UIScreen mainScreen].bounds.size.height==736)// iPhone 6 Plus 66 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,950)];
}
else if ([UIScreen mainScreen].bounds.size.height==667)// iphone 6 44 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,900)];
}
else if ([UIScreen mainScreen].bounds.size.height==568)// iphone 5, 5C, 5S 44 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,800)];
}
else // iphone 4, 4S 44 Navigation ba
{
[ScrollVc setContentSize:CGSizeMake(320,750)];
}
}
请根据您的要求更改必要的值。
希望这有帮助。