스크롤보기로 동적 버튼과 라벨을 만들고 있는데 자동 레이아웃을 설정하고 싶습니다. 가능한 여러 개의 동적 버튼을 설정합니다 .i 많은 자습서를 검색하지만 자동 레이아웃 설정을위한 예제가 없습니다. 여러 동적 버튼. 작업은하지만 모든 버튼이 함께 설정됩니다. 단 하나의 버튼과 레이블 만 표시합니다. 하지만 나는 정확한 결과를 검색하고 있지만 자동 레이아웃은 작동하지 않습니다. 무슨 문제동적으로 생성 된 버튼에 자동 레이아웃을 설정하는 방법은 무엇입니까?
-(void)DynamicButton:(NSMutableArray*)objectName
{
for(UIView *view in [scrollView subviews])
{
[view removeFromSuperview];
}
int yPossion = 100, xPossion = 44; int temp = 0;
for (int i = 0; i<[objectName count]; i++)
{
SMSCategory *cat = [objectName objectAtIndex:i];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:i];
[aButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setBackgroundImage:[UIImage imageNamed:@"icon-menu.png"]
forState:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:@"%d",i]
forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
aButton.highlighted=YES;
[scrollView addSubview:aButton];
;
xPossion += aButton.frame.size.width+35;
temp++;
if (temp==3)
{
yPossion = aButton.frame.origin.y+aButton.frame.size.height+20;
temp = 0;
xPossion = 44;
yPossion += aButton.frame.size.width-15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width ,yPossion-
50)];
}
UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:YES];
[label setText:cat.Name];
[label setTextColor:[UIColor blackColor]];
label.font = [UIFont systemFontOfSize:12];
[label sizeToFit];
[label setFrame:CGRectMake(4, 44, 70, 60)];
[scrollView addSubview:label];
[aButton addSubview:label];
}
}
//Autolayout code
[aButton setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *viewsDictionary = @{@"aButton":aButton};
// 2. Define the button Sizes
NSArray *aButton_constraint_H = [NSLayoutConstraint
constraintsWithVisualFormat:@"V:[aButton(60)]"
options:0
metrics:nil
views:viewsDictionary];
NSArray *aButton_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[aButton(70)]"
options:0
metrics:nil
views:viewsDictionary];
[aButton addConstraints:aButton_constraint_H];
[aButton addConstraints:aButton_constraint_V];
// 3. Define the views Positions using options
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[aButton]"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_POS = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[aButton]"
options:0
metrics:nil views:viewsDictionary];
[self.view addConstraints:constraint_POS_V];
[self.view addConstraints:constraint_POS];
}
https://github.com/smileyborg/PureLayout 예를 살펴보십시오. – pqteru
감사하지만, 몇 가지 간단한 예제가 있습니다 –
문제는 모든 버튼이 함께 설정되어 마지막 버튼 만 표시되지만 버튼을 검색 할 때 올바른 결과가 표시된다는 것입니다. 무엇이 문제입니까 –