2016-05-05 3 views
0

Я делаю пейджинговый вид scrollview. Он отлично работает, когда я даю ширину и высоту подшивки.UIScrollView делает динамическую высоту и ширину subviews contectview программно

позвольте мне показать вам код.

Изготовление Scrollview и contenview

[self.view addSubview:self.scrollView]; 
[self.scrollView addSubview:self.contectView]; 

Addding constraing для Scrollview и contenview

NSDictionary *views = @{@"scrollView" : self.scrollView , @"contectView" : self.contectView }; 

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:views]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:views]]; 

[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contectView]|" options:0 metrics:0 views:views]]; 
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contectView]|" options:0 metrics:0 views:views]]; 

Добавление подвид в contectview

for (int i=0; i<10; i++) { 

    UIView *productView =[self creatProductView]; 
    [self.contectView addSubview:productView]; 

NSDictionary *views = @{@"productView" : productView , @"contectView" : self.contectView }; 
    if (i==0) { 
     [self.contectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[productView(==375)]" options:0 metrics:0 views:views]]; 
    } 
    else{ 
     views = @{@"productView" : productView , @"contectView" : self.contectView , @"lastView" : lastView , @"view" : self.view}; 
     [self.contectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lastView][productView(==375)]" options:0 metrics:0 views:views]]; 

    } 
    [self.contectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[productView(==667)]" options:0 metrics:0 views:views]]; 
    lastView = productView; 
} 

Constraint мне нужно изменить:

H:|[productView(==375)] 
H:[lastView][productView(==375)] 
V:|[productView(==667)] 

Как выше я дал фиксированное ограничение. Я не могу дать относительно contectView, потому что он не имеет ограничений по ширине и высоте

+0

Вы проверили вид Визуализируйте на тренажере с Xcode -> Выберите Debug> Просмотр Debugging> Show Alignment Прямоугольники. Runtime изображение может сделать его более понятным. – kaushal

ответ

1

Я думаю, что это то, что вы хотите.

self.scrollView = [UIScrollView new]; 
self.contectView = [UIView new]; 
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
self.contectView.translatesAutoresizingMaskIntoConstraints = NO; 

[self.view addSubview:self.scrollView]; 
[self.scrollView addSubview:self.contectView]; 

NSDictionary *views = @{@"scrollView" : self.scrollView , @"contectView" : self.contectView }; 

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:views]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:views]]; 

//Adding equal width and height constraint with LOW priority 
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contectView([email protected])]|" options:0 metrics:0 views:views]]; 
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contectView([email protected])]|" options:0 metrics:0 views:views]]; 

NSString *horizontalConstraintsFormat = @""; 
NSMutableDictionary *subViews = [NSMutableDictionary new]; 
subViews[@"scrollView"] = self.scrollView; 
int count = 10; 
for (int i = 0; i < count; i++) { 
    //Create your content view, e.g. 
    UIView *productView = [UIView new]; 
    productView.backgroundColor = [UIColor colorWithWhite:arc4random()%100/100.0f alpha:1]; 

    //Important line! Disable translation of autoresizing mask into constraints 
    productView.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.contectView addSubview:productView]; 

    NSString *key = [NSString stringWithFormat:@"productView%d", i]; 
    subViews[key] = productView; 
    if (i == 0) { 
     //pin to the left side and set equal width to scrollview 
     horizontalConstraintsFormat = [horizontalConstraintsFormat stringByAppendingFormat:@"|-0-[%@(==scrollView)]", key]; 
    } else if (i == count - 1) { //is the last one 
     //pin to the prev item and right side and set equal width to scrollview 
     horizontalConstraintsFormat = [horizontalConstraintsFormat stringByAppendingFormat:@"-0-[%@(==scrollView)]-0-|", key]; 
    } else { 
     //pin to the prev item and set equal width to scrollview 
     horizontalConstraintsFormat = [horizontalConstraintsFormat stringByAppendingFormat:@"-0-[%@(==scrollView)]", key]; 
    } 

    //Set equal height to scrollview 
    NSString *verticalConstraintsFormat = [NSString stringWithFormat:@"V:|[%@(==scrollView)]", key]; 
    [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalConstraintsFormat options:0 metrics:nil views:subViews]]; 
} 

//applying generated format 
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontalConstraintsFormat options:0 metrics:nil views:subViews]]; 

РЕЗУЛЬТАТ = >>
enter image description here

+1

Прохладный .. это именно то, что я хочу .. –

Смежные вопросы