1

I am adding scrollView using constraints and it is working right. But it is showing strange behaviour.

When I am adding frame of scrollView as [SBV setFrame:CGRectMake(0, 0, scrllView.frame.size.width,1000)]; then its color is clearColor always no matter what I am givig to it.

Code:

UIScrollView *scrllView=[UIScrollView new];
[scrllView setTranslatesAutoresizingMaskIntoConstraints:NO];

[self.view addSubview:scrllView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];


[scrllView setContentSize:CGSizeMake(self.view.frame.size.width, 1000)];


UIView *SBV=[UIView new];
[SBV setTranslatesAutoresizingMaskIntoConstraints:YES];

[SBV setFrame:CGRectMake(0, 0, scrllView.frame.size.width,1000)];
[SBV setBackgroundColor:[UIColor grayColor]];

[scrllView addSubview:SBV];

When I am adding frame of view as [SBV setFrame:CGRectMake(0, 0,self.view.frame.size.width,1000)]; then its color is same as given my me. Gray in this case

Bhupesh Kumar
  • 369
  • 2
  • 18
  • theres a rule with `UIScrollView` and auto layout, `UIScrollview` needs to be able to calculate its content size, add one subview to the scrollview and add your views inside that and constrain them. – cream-corn Sep 10 '15 at 05:51
  • As I have written in bold that it is working correct except that color that I am giving to it. @cream-corn – Bhupesh Kumar Sep 10 '15 at 06:00

2 Answers2

1

Don't use CGRectMake function instead leave the size as it is. It wont show any strange behaviour.

irudaya rajasekar
  • 924
  • 1
  • 8
  • 16
  • `SBV` is the new instance of `UIView`. What will be its size if i will not use cgrectmake method. ?@irudaya and Also If i am removing the cgrectmake . its not showing the color of view again. there must be some another reason – Bhupesh Kumar Sep 10 '15 at 05:37
  • Then use size classes and try making the layout guidelines instead of using auto layout – irudaya rajasekar Sep 10 '15 at 05:40
0

Setting content size when using Autolayout does have no effect.

Please refer to this answer - UIScrollView contentSize not working

Also this - UIScrollView doesn't use autolayout constraints

Community
  • 1
  • 1
Dvole
  • 5,725
  • 10
  • 54
  • 87