Как программно настроить подпредставления с помощью AutoLayout в UIScrollView?
Я создаю приложение с UiScrollview и UIPagectontrol, используя Autolayout программно, для
Я создалTKScroller как подкласс UIView, я инициирую его, используя Some Mode и Array.
TKScroller.m
-(void)setData{
[self layoutIfNeeded];
CGRect mainFrame=self.frame;
[self layoutIfNeeded];
CGRect mainFrame=self.frame;
UIView *lastview;
NSMutableArray* manualConstraints = [NSMutableArray array];
for (int i=0; i<arrayData.count ; i++)
{
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
UIView *subview = [UIView new];
subview.backgroundColor = [self getRandomColor];
[scrollView addSubview:subview];
if (i==0)
{
NSLayoutConstraint* b1_top = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:5];
[manualConstraints addObject:b1_top];
NSLayoutConstraint* b1_left = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1 constant:5];
[manualConstraints addObject:b1_left];
NSLayoutConstraint* b1_right = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-5];
[manualConstraints addObject:b1_right];
NSLayoutConstraint* b1_bottom = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:-10];
[manualConstraints addObject:b1_bottom];
[subview layoutIfNeeded];
[scrollView addConstraints:manualConstraints];
lastview=subview;
}
else{
NSLayoutConstraint* b1_top = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:5];
[manualConstraints addObject:b1_top];
NSLayoutConstraint* b1_left = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:lastview attribute:NSLayoutAttributeLeading multiplier:1 constant:5];
[manualConstraints addObject:b1_left];
NSLayoutConstraint* b1_right = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-5];
[manualConstraints addObject:b1_right];
NSLayoutConstraint* b1_bottom = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:-10];
[manualConstraints addObject:b1_bottom];
[subview layoutIfNeeded];
[scrollView addConstraints:manualConstraints];
lastview=subview;
}
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * arrayData.count, mainFrame.size.height/2);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = arrayData.count;
pageControlBeingUsed = NO;
}
-(UIColor *)getRandomColor{
int r = arc4random() % 255;
int g = arc4random() % 255;
int b = arc4random() % 255;
return [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0];
}
Прямо сейчас я получаю любые подпредставления,
но если я изменю ориентацию, это даст неверный результат, так как я могу датьNSLayoutConstraint
для подпредставлений scrollview?
РЕДАКТИРОВАТЬ
После добавленияNSLayoutConstraint
подпредставления не показаны. Я просто пропускаю некоторые ограничения, пожалуйста, исправьте меня в динамических настройках ограничений.
мойИсходный код здесь , Помогите мне, пожалуйста. Спасибо. И извините за плохую грамматику.