I have a customView created in xib with height = 800 :
I want to call it via addSubview into the blue view as shown in the UIViewController :
the problem here is I want my UIScrollView can dynamically set it's contentSize, because here I define it's height to 800 :
I read from another article that the contentSize in AutoLayout will be defined by the height of its subView, in this case the contentView. but still after I set the height to 800 the scroll is not reaching the bottom of the view (pink). how to set the autolayout then?
here is my code in the customView :
- (id)initWithFrame:(CGRect)frame
{
NSLog(@"initWithFrame");
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
NSLog(@"initWithCoder");
self = [super initWithCoder:aDecoder];
if(self) {
[self setup];
}
return self;
}
- (void)setup {
[[NSBundle mainBundle] loadNibNamed:@"customView" owner:self options:nil];
[self addSubview:self.view];
NSLog(@"contentSize Height :%f", self.myscrollview.contentSize.height);
NSLog(@"contentView Height :%f", self.contentView.frame.size.height);
}
in my ViewController.m i called it like this :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
customView *customV = [[customView alloc] initWithFrame:self.wantToShowHereView.bounds];
[self.wantToShowHereView addSubview:customV];
}
Can someone give me direction on how I want to achieve it? Here is my example project to make it clear for you : AutoLayoutScrollView Example




Couple of issues.
First, you are setting the frame of
customVto the bounds ofwantToShowHereView-- but you're doing so inviewDidLoad(). That bounds will almost certainly change betweenviewDidLoad()and the time you actually see it on screen (device size, orientation, etc).Second,
customVis aUIViewonto which you are adding the XIB's "root view" (which contains your scroll view) as a subview tocustomV... but you're not setting any constraints (or other resizing behaviors) on that view.Third, you're mixing relative constraints with absolute constraints (widths, heights, leading, trailing, etc), which will again cause issues when the overall frame changes... and you're explicitly setting the frame of
customVinstead of adding constraints at run-time.You can get a start on fixing things:
Step one - remove
customVinstantiation fromviewDidLoad().Step two - add the following
viewDidAppear()method.Doing only that should give you a properly scrollable view.
What you probably want to do, though, is keep the init in
viewDidAppear()but add constraints there to make use of auto-layout.Also, I'd recommend re-working the constraints on the elements in your
customView.xibso the scrolling (the contentSize) is determined by the actual content of your scroll view, not by hard-coding a height of yourcontentView.Edit:
Here is how your
viewDidLoadcould look (inViewController.m):and
setupincustomView.m: